簡體   English   中英

字符串格式的一個小數

[英]one decimal for string format

我的數字低於數字。 我想在小數點后顯示一位數。 如何格式化?

2.85
2
1.99

我正在使用(“{0:0.0}”。但數據顯示為

2.9 //It should be 2.8
2.0 //It should be 2
2.0 //It should be 1.9

嘗試使用"{0:0.#}"作為格式字符串。 但是,這只會修復.0 要將舍入修復為始終向下舍入,您可能需要使用:

string s = (Math.Floor(value * 10) / 10).ToString("0.#");
Decimal[] decimals = { new Decimal(2.85), new Decimal(2), new Decimal(1.99) };

foreach (var x in decimals)
{
  Console.WriteLine(string.Format("{0:0.#}", Decimal.Truncate(x * 10) / 10));
}

// output
2.8
2
1.9

暫無
暫無

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

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