简体   繁体   English

四舍五入字典中的浮点值

[英]Round off float values in a dictionary

I have an array containing dictionaries. 我有一个包含字典的数组。 I need to change the value for key "price" to float value with 2 decimal places alone, but the dictionary should remain the same. 我需要将键“ price”的值更改为仅带有2个小数位的浮点值,但字典应保持不变。 I tried fetching each price values and replacing them into a mutable array. 我尝试获取每个价格值并将其替换为可变数组。 But I need it in a simpler solution. 但是我需要一个更简单的解决方案。 Thanks in advance. 提前致谢。

(
{
    pen =     {
        price = 180.1299;
    };
    pencil =     {
        price = 20.1299;
    };
},
{
    pen =     {
        price = 111.1267;
    };
    pencil =     {
        price = 23.1278;
    };
}
)

Simple solutions 简单的解决方案

float  price = 180.1299;
NSString *strRoundPriceVal = [NSString stringWithFormat:@"%.2f",price];
NSLog(@"The Rounded Price Value is - %@",strRoundPriceVal);

Output 产量

在此处输入图片说明

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

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