how can I control the no. of numbers after decimal point? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

how can I control the no. of numbers after decimal point?

for example printing .12 instead of .123

12th Jul 2016, 4:29 AM
frizern
frizern - avatar
2 Answers
+ 1
u can use std::setprecision(2) -- include iomanip.h or std::round(.5) -- include math.h. there is also floor and truncate function. u can write ur own function or import any 3rd party written function and use.
12th Jul 2016, 11:36 AM
VISHAL PRAJAPATI
VISHAL PRAJAPATI - avatar
0
The question is: What for? If it's just for output and you would like to keep the fraction part in the variable untouched, use stream modifiers that just modify how cout prints them to the standard output. If you really want less precision, you can either chose float or double or cut the fraction part off via mathematical operations.
12th Jul 2016, 8:20 AM
Stefan
Stefan - avatar