#151860 - darkchild - Thu Mar 06, 2008 1:42 am
Let's say I have a float, but instead of having:
0.000000000000000000000000000
I want
0.00
or even round it up to:
0
is it possible? if so, how to? (If there is a function tell me, if not I'll do it manually :3)
#151865 - furrykef - Thu Mar 06, 2008 2:11 am
This should have been posted in the C/C++ forum, since it doesn't have anything to do with the Nintendo DS in particular.
Anyway, it depends. If you're using printf, you can do like this:
printf("%.02f", myfloat);
This will cause it to print two digits after the decimal point, including trailing zeroes. Consult the documentation on printf for more information about what this does and what else you can do.
If you're using cout in C++, do like this:
cout << setiosflags(ios::fixed) << setprecision(2) << myfloat;
I dunno if this will print trailing zeros or not. Either try it or consult the documentation to find out ;)
- Kef
#151884 - nczempin - Thu Mar 06, 2008 11:16 am
furrykef wrote: |
This should have been posted in the C/C++ forum, since it doesn't have anything to do with the Nintendo DS in particular.
Anyway, it depends. If you're using printf, you can do like this:
printf("%.02f", myfloat);
This will cause it to print two digits after the decimal point, including trailing zeroes. Consult the documentation on printf for more information about what this does and what else you can do.
If you're using cout in C++, do like this:
cout << setiosflags(ios::fixed) << setprecision(2) << myfloat;
I dunno if this will print trailing zeros or not. Either try it or consult the documentation to find out ;)
- Kef |
The question is a bit confusing. Of course there's no rounding involved in getting from 0.000000... to 0.0, so it's not really a maths question. So it's either about printing, in which case the previous poster answered it.
Or it's about rounding arbitrary floats to arbitrary precision. I don't have all the C libraries memorized, but if there is not a round() function, there usually is a floor (or similar) function, and usually adding 0.5 or something similar at a different precision and then cutting off the rest does the trick.
And I certainly agree that this has nothing to do with DS dev, and should be moved.
#153522 - sammyrp - Tue Apr 01, 2008 10:47 am
If you want it to 2 decimal places then you can add whats below. I'm new to programming so this might be what you mean by manualy.
Code: |
std::cout.setf(std::ios_base::fixed);
std::cout.setf(std::ios_base::showpoint);
std::cout.presision(2); |
#153785 - sgeos - Sun Apr 06, 2008 7:26 am
Look into the mathematical floor, ceiling and modulus functions. C has versions of all of them, and I suspect they work with floats. =)
-Brendan