简体   繁体   中英

NSNumberFormatter and SpellOutStyle with Float Values

I am trying to render a float value using NSNumberFormatter's SpellOutStyle, but the results are unsatisfactory, and I haven't been able to find a solution so far. The issue is, when given a float value such as 3.2, NSNumberFormatterSpellOutStyle renders it as something like this: "three point two zero zero zero zero zero zero four seven six eight three seven two".

When using the SpellOutStyle, setting the roundingMode or maximumFractionDigits properties don't seem to have any effect.

Here is a sample of how I am running the code:

NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setNumberStyle:NSNumberFormatterSpellOutStyle];
NSNumber *numberValue = [NSNumber numberWithFloat:3.2];

NSLog(@"Number: %@", [numberFormatter stringFromNumber:numberValue]);
// Outputs three point two zero zero zero zero zero zero ...

[numberFormatter release];

Any ideas?

The problem is that floats are imprecise. Try changing line 3 to:

NSNumber *numberValue = [NSNumber numberWithDouble:3.2];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM