Re: Formatting float numbers
Re: Formatting float numbers
- Subject: Re: Formatting float numbers
- From: Andreas Grosam <email@hidden>
- Date: Wed, 25 Aug 2010 15:20:09 +0200
On Aug 23, 2010, at 1:35 AM, Adriano Brandão wrote:
> Thanks man, I tested here and it's working now.
>
> But, just for curiosity, if I was using only c++ wouldn't I be able to do that?
Using the C and C++ standard libraries only, you can't do that with the provided formatters.
With the c-style "fF" conversions, the precision format specifies the number of digits after the decimal point. There may be trailing zeros.
With the c-style "gG" conversion, the precision format specifies the maximum number of significant digits to display, which is the total of the number of digits before and after the decimal point. Trailing zeros after the decimal point are stripped, and possibly the decimal point if there are only zeros following.
This is equivalent for the iostream library where you set the precision using the stream manipulator: setprecision().
In other words, there is no format specifier, which uses n digits after the decimal point, and also strips the trailing zeros and possibly the decimal point. Although the "gG" conversion strips trailing zeros after the decimal point, it does not work for your problem, since it specifies the "maximum" number of significant digits - you can't use it to specify n digits after the decimal point.
>
>
> On 22/08/2010, at 19:04, Greg Parker wrote:
>
>> On Aug 21, 2010, at 9:59 AM, Adriano Brandão wrote:
>>> I'm having some troubles in formatting float numbers. If I wish to display 25.342 using only 2 decimal places I could use %.2f and it would do the job. But if the number to be formatted is flat (like 25.000) I get 25.00 as a result.
>>> After a little bit of research I found out that I could use # as a flag (%.#2f). But, even in this case, I get 25. (with the dot). So, what's the right way for displaying only 25 (mantaining the 2 decimal places for numbers that use them)?
>>
>> You can't get that result directly from printf(3), but you can do it with NSNumberFormatter:
>>
>> {
>> NSNumberFormatter *fmt = [[NSNumberFormatter alloc] init];
>> [fmt setFormat:@"0.##"];
>> NSLog(@"%@", [fmt stringFromNumber:[NSNumber numberWithFloat:25.342]]);
>> NSLog(@"%@", [fmt stringFromNumber:[NSNumber numberWithFloat:25.3]]);
>> NSLog(@"%@", [fmt stringFromNumber:[NSNumber numberWithFloat:25.0]]);
>> }
>>
>> 2010-08-22 15:04:10.614 a.out[6954:903] 25.34
>> 2010-08-22 15:04:10.616 a.out[6954:903] 25.3
>> 2010-08-22 15:04:10.617 a.out[6954:903] 25
>>
>>
>> --
>> Greg Parker email@hidden Runtime Wrangler
>>
>>
>
> _______________________________________________
>
> Cocoa-dev mailing list (email@hidden)
>
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden
-------------------------------
Andreas Grosam
Eisenbahnstr. 27
D-79650 Schopfheim
phone: +49 7622 1741
phone/Fax: +49 7622 697 639 0
email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden