How To Round Off A Number

Sometimes, it is needed to round off a number to some decimal digits.  Again stringstream comes to help us with the aid of iomanip function setprecision. Following is a sample function to round off a number to a fixed digits.

double Round(const double value, const int digits)
{
   stringstream stream;
   // Store the number with required no. of decimal
   // places to stream
   stream << setprecision(digits) << value;
   // Convert stream to number
   double roundedValue = 0.0;
   stream >> roundedValue;
   return roundedValue;
}

The header files sstream and iomanip are needed for the above.

Please note that the variable value holds the number with original no. of decimal places and digits is the total number of digits needed (including the non-decimal digits) after rounding has taken place.

Round(10.37665, 3) gives 10.4 and Round(10.3745, 4) gives 10.37.

Note

If you want to store the number in scientific notation in stream, you can do

stream <<  scientific;

For fixed point format, do

stream <<  fixed;

33 Responses to How To Round Off A Number

  1. Mike says:

    Just passing by.Btw, your website have great content!

    _________________________________
    Making Money $150 An Hour

  2. Marinkina says:

    Пора переименовать блог, присвоив название связанное с доменами 🙂 может хватит про них?

  3. Gavrilin says:

    Пора переименовать блог, присвоив название связанное с доменами 🙂 может хватит про них?

  4. Cederash says:

    Даже и не придирешься!

  5. Avertedd says:

    Спасибо за пост. Позновательно.

  6. Эдуард says:

    Занятно. Хотелось бы еще чего-нибудь об этом же.

  7. BepcaджE says:

    Действительно, на самом деле все очень просто 🙂

  8. Увлекательно было почитать 🙂 Попробуем-с тоже ответить в ближайших постах.

  9. humwtxs says:

    Bwc6RO fydyocpgeeko, [url=http://xehytqbdhgjr.com/]xehytqbdhgjr[/url], [link=http://ygxnqifveena.com/]ygxnqifveena[/link], http://zrsjmgbkezcf.com/

  10. Kardebaron says:

    Оральный секс – это рассказы приятелям о сексуальных успехах, якобы имевших место…

  11. Karlusha_AG says:

    Вспыхает небо, разбужая ветер,
    Проснувший гомон птичьих голосов.
    Проклинывая всё на белом свете,
    Я вновь бежу в нетоптаность лесов.
    Шуршат зверушки, выбегнув навстречу,
    Приветливыми лапками маша:
    Я среди тут пробуду целый вечер
    Бессмертные творения пиша
    Но, выползя на миг из тины зыбкой,
    Болотная зелёновая тварь
    Совает мне с заботливой улыбкой
    БОЛЬШОЙ ОРФОГРАФИЧЕСКИЙ СЛОВАРЬ

    Люди, учите Русский Язык и пишите ПРАВИЛЬНО.

  12. Keftab says:

    News. Todays Special Offer Keftab. No Prescription Required. Cheap drugs.

Leave a comment