English 中文(简体)
为什么要贴上标本?
原标题:Why aren t the decimals printed to the string?

为了获得2个精度的护卫,我尝试:

   [[NSString stringWithFormat:@"%.2f",[[self CurrentValue] doubleValue]]]

页 次

   [self CurrentValue] stringValue]

and 页 次:

    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
    formatter.numberStyle = NSNumberFormatterCurrencyStyle;
    NSString *string = [formatter stringFromNumber:[self CurrentValue]];
    [formatter release];

但它没有工作。 THe原为浮体=22,我总是得到“22”而不是“22.00”。

增 编

最佳回答

我有几种试验情景,希望这能够帮助你摆脱危机。 如果你使用一种货币,则模式是理想的。 从这个例子出发,你可以设立编号——NSNumber* number = [自现在起Value];

NSNumber * number = [NSNumber numberWithInt:22];
NSString * string1 = [NSString stringWithFormat:@"%.2f",[number doubleValue]];
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
formatter.numberStyle = NSNumberFormatterCurrencyStyle;
NSString *string2 = [formatter stringFromNumber:number];
[formatter release];

NSLog(@"string 1 %@
string 2 %@
string 3 %@", string1, string2, [number stringValue]);

//output
string 1 22.00
string 2 $22.00
string 3 22

// top code with NSNumber * number = [NSNumber numberWithFloat:22];
string 1 22.00
string 2 $22.00
string 3 22

// top code with NSNumber * number = [NSNumber numberWithFloat:22.0];
string 1 22.00
string 2 $22.00
string 3 22

Summary: The way the number is created is not significant here to the output if it is truly an int (floats with such as 4.20 will work as expected in every case, but every int value 22,22.0,22.000 gets treated the same by all 3 ways of creating a number. So choose the format you like best and implement that.

问题回答

页: 1 你可以尝试

[NSString stringWithF或mat:@"%.2f”,[[自现在起Value]potValue]

[NSString stringWithF或mat:@"%.2lf",[[self CurrentValue] doubleValue]] both are good to go.

It actually much simpler. You said that "The original number is a float = 22". Now, remember that obj-c runtime class may differ from declared one. And when you instantiate your float variable with actually integer value - it is an integer one at runtime! You should change it to one of the following:
float someFloatValue = 22f;
float someFloatValue = 22.0;
float someFloatValue = (float)22; (not sure about that one thought)

1. 幸福套





相关问题
Asynchronous request to the server from background thread

I ve got the problem when I tried to do asynchronous requests to server from background thread. I ve never got results of those requests. Simple example which shows the problem: @protocol ...

objective-c: Calling a void function from another controller

i have a void, like -(void) doSomething in a specific controller. i can call it in this controller via [self doSomething], but i don t know how to call this void from another .m file. I want to call ...

ABPersonViewController Usage for displaying contact

Created a View based Project and added a contact to the AddressBook using ABAddressBookRef,ABRecordRef now i wanted to display the added contact ABPersonViewController is the method but how to use in ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

NSUndoManager and runModalForWindow:

I have a simple Core Data app which displays a list of entities in the main window. To create or add new entities, I use a second modal window with a separate managed object context so changes can be ...

NSMutableArray values becoming "invalid"

I m trying to show a database information in a tableview and then the detailed information in a view my problem is as follow: I created a NSMutableArray: NSMutableArray *myArray = [[NSMutableArray ...

iPhone numberpad with decimal point

I am writing an iPhone application which requires the user to enter several values that may contain a decimal point (currency values, percentages etc.). The number of decimal places in the values ...

热门标签