簡體   English   中英

四舍五入字典中的浮點值

[英]Round off float values in a dictionary

我有一個包含字典的數組。 我需要將鍵“ price”的值更改為僅帶有2個小數位的浮點值,但字典應保持不變。 我嘗試獲取每個價格值並將其替換為可變數組。 但是我需要一個更簡單的解決方案。 提前致謝。

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

簡單的解決方案

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

產量

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM