float f = [str floatValue];
Why f is 4.030000002 behind where so many decimal places?
Or ask how to convert after 4.03
Reply:
long tmp = f * 1000 ;/ / magnification of 1000 after taking decimal three
f = tmp / 1000.0;
Reply:
The result is 4, not 4.03, but this felt Rao circle, ask what methods do
Reply:
float f = 4.3020003;
long tmp = f * 1000;
f = tmp / 1000.0;
cout << f << endl;
Do not forget / 1000.0 is not in addition to 1000, except for 1000 will be 4
This is the easiest way
Reply:
If you are the output, you can use sprintf ("% .3 f", f);
Reply:
printf ("% .3 f", f);
Reply:
Hello, the result was divided by 1000.0 4.0300002
Reply:
Not output, I was saved to the variable inside, behind the operation to be performed.
Reply:
long tmp = f * 1000; This is the key, first converted to plastic, remove the fractional part of the
Reply:
f = tmp / 1000.0;
Reply:
Yes, my test code
NSString * s = @ "4.03";
float f = [s floatValue];
long l = f * 1000;
f = l / 1000.0;
By breakpoints, f is 4.0300002
Reply:
According to my way of doing certainly no problem, if you are afraid of overflow to 64 of the best plastic
quint64 tmp = f * 1000;
Reply:
What is your development environment
Reply:
I vc + +6.0 / / gcc is 4.03
Reply:
I was xcode5, iOS sdk7.0
Reply:
4.302
Press any key to continue
This is my vc6.0 output
# Include
# Include
void main ()
{
float f = 4.3020003;
long tmp = f * 1000;
f = tmp / 1000.0;
/ / Printf ("% .3 f", f);
cout << f << endl;
}
Reply:
L see whether you are tuning 4302
Reply:
Or 4.0300002
Reply:
That explanation or statement in front of the floating-point l, ah, how not an integer
Reply:
Forced conversion try, long tmp = (long) (f * 1000);
Reply:
/ / Use sprintf and sscanf achieve it.
char buf [256];
float f = 4.030002;
snprintf (buf, sizeof (buf), "% .3 f", f) ;/ / The key is to use the format function
snprintfsscanf (buf, "% f", & f);
No comments:
Post a Comment