简体   繁体   中英

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. 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

在此处输入图片说明

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