简体   繁体   English

如何从NSDictionary获取NSDecimalNumber

[英]How to get NSDecimalNumber from NSDictionary

I want to set the value for Key1 as 10.00 itself rather than 10 我想将Key1的值本身设置为10.00而不是10

            NSDictionary *dict = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:
                                 [NSDecimalNumber decimalNumberWithString:@"10.00"],
                                 [NSDecimalNumber decimalNumberWithString:@"10.19"],nil]
                                 forKeys:[NSArray arrayWithObjects:@"Key1",@"Key2",nil]];


            NSLog(@"Value For Key1 NSDecimalNumber %@",[dict valueForKey:@"Key1"]);
            NSLog(@"Value For Key1 float %f",[[dict valueForKey:@"Key1"] floatValue]);

            NSLog(@"Value For Key2 NSDecimalNumber %@",[dict valueForKey:@"Key2"]);
            NSLog(@"Value For Key2 float %f",[[dict valueForKey:@"Key2"] floatValue]);

This is the log that I get from the console 这是我从控制台获得的日志

    2013-05-16 12:43:23.316 SampleTest[6569:19d03] Value For Key1 NSDecimalNumber 10
    2013-05-16 12:43:23.771 SampleTest[6569:19d03] Value For Key1 float 10.000000
    2013-05-16 12:43:24.263 SampleTest[6569:19d03] Value For Key2 NSDecimalNumber 10.19
    2013-05-16 12:43:25.195 SampleTest[6569:19d03] Value For Key2 float 10.190000

Can anyone help me out??? 谁能帮我吗???

Thanks in advance.. 提前致谢..

If you just need a formatted string with precision for decimal values. 如果您只需要一个格式化的精度为十进制值的字符串。 You can use 您可以使用

NSString *str = [NSString stringWithFormat:@"%.2f",[[dict valueForKey:@"Key1"] floatValue]];
NSLog(@"str - %@",str);

str will always have the string with precision of 2 digits. str始终具有2位数精度的字符串。

Hope this helps and is simple. 希望这会有所帮助并且很简单。

If I understand you correctly you want to control the output format of your NSDecimalNumber object. 如果我理解正确,则您想控制NSDecimalNumber对象的输出格式。
Cocoa provides the NSNumberFormmatter class for that purposes. 为此,可可提供了NSNumberFormmatter类。 It allows you to customize the string representation of your number without modifying the actual value. 它允许您自定义数字的字符串表示形式,而无需修改实际值。

NSDecimalNumber is a good choice if you have to deal with numbers that require high precision. 如果必须处理要求高精度的数字,则NSDecimalNumber是一个不错的选择。 Eg when working with currencies. 例如,使用货币。

I modified your sample to output a formatted version of the first value stored in the dict : 我修改了示例以输出存储在dict中的第一个值的格式化版本:

NSDictionary* dict = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:
                                                          [NSDecimalNumber decimalNumberWithString:@"10.00"],
                                                          [NSDecimalNumber decimalNumberWithString:@"10.19"],nil]
                                                 forKeys:[NSArray arrayWithObjects:@"Key1",@"Key2",nil]];
NSNumberFormatter* formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
[formatter setAllowsFloats:YES];
[formatter setAlwaysShowsDecimalSeparator:YES];
[formatter setFormat:@"#,##0.00"];
NSString* formattedValue = [formatter stringFromNumber:[dict valueForKey:@"Key1"]];
NSLog(@"Value For Key1 %@",formattedValue);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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