![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#11 |
|
Programmer
Join Date: Feb 2005
Posts: 41
Rep Power: 0
![]() |
I think even if I do this:
number float; number = stof (printf("%.2f\n", number)); not sure it stof is string to float, but anyway I would still have all digits with zeros right? Can I just set it to 2 digits lik 44.23? maybe I have to change it to short or whatever! thx for the help guys. |
|
|
|
|
|
#12 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
You can use the round, roundf and roundl functions to round numbers to the nearest integer. Given that, we can use simple multiplication and division to round to x decimal places.
float number[3] = { 89.27218, 0, 0 };
number[1] = round(number[0]); // 89
number[2] = round(number[0] * 100) / 100); // 89.27 |
|
|
|
|
|
#13 |
|
Hobbyist Programmer
Join Date: Oct 2005
Location: Ohio
Posts: 177
Rep Power: 0
![]() |
Another way to do it would be to mask out all but the first 2 digits in the decimal part of the number. This would require knowledge of how a float is stored on your machine.
|
|
|
|
|
|
#14 | |
|
Professional Programmer
![]() Join Date: Sep 2005
Posts: 419
Rep Power: 4
![]() |
Quote:
#include <stdio.h>
#include <math.h>
int main ( void )
{
double a = 87.99876;
double b = 87.98876;
const double fudge = .01;
if ( fabs ( a - b ) <= fudge )
puts ( "Identical with fudge" );
else
puts ( "Different" );
a = 87.99876;
b = 87.99765;
if ( fabs ( a - b ) <= fudge )
puts ( "Identical with fudge" );
else
puts ( "Different" );
return 0;
}
__________________
Even if the voices aren't real, they have some pretty good ideas. |
|
|
|
|
|
|
#15 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
There are actual quantities and then there are representations of quantities and the failings thereof. Grab your hand calculator and compare 1/3*3 with 1*3/3. Everyone knows they're the same (except your calculator). Another kind of problem is that floating point methods store a much larger range of numbers in the same space as integers with a much smaller range. A second's thought will raise suspicions that something had to go, in the tradeoff. Your machine likes to scale numbers using powers of 2, whereas most of us like to scale numbers using powers of 10. Things won't always work out cleanly.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#16 | |
|
Hobbyist Programmer
Join Date: Oct 2005
Location: Ohio
Posts: 177
Rep Power: 0
![]() |
Quote:
|
|
|
|
|
|
|
#17 | |
|
Professional Programmer
Join Date: May 2006
Location: Maryland, USA
Posts: 306
Rep Power: 3
![]() |
Quote:
.*Whips out TI-84* 1/3*3 = 1 1*3/3 = 1 It works even if you store 1/3 in a variable then multiply by 3. Gotta love the TI-84, but your point is still true, its just not the best example. |
|
|
|
|
|
|
#18 | |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Quote:
![]()
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
|
#19 | |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,223
Rep Power: 5
![]() |
Quote:
|
|
|
|
|
|
|
#20 |
|
Programmer
Join Date: Feb 2005
Posts: 41
Rep Power: 0
![]() |
Well, this all started couse I can mak a number like 83.389999
become a 83.39 (as a string), but when I put it back to float it becomes 83.390002, and that is all I dont want it. I guess I can not use float, what is the kind I should use for 83.39? |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|