Round off float values in a dictionary

时间:2017-08-04 13:11:06

标签: ios objective-c floating-point nsmutablearray nsmutabledictionary

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. 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;
    };
}
)

1 个答案:

答案 0 :(得分:0)

Simple solutions

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

Output

enter image description here