我得到了一個價格小數點,有時可以是 0.00002001 或 0.00002。如果數字像 0.00002,我想總是從右邊顯示 3 個零,所以我希望它是 0.00002000。如果數字是 0.00002001,則不要添加任何內容。我在 msdn 中遇到了一些示例 和其他示例并嘗試使用
price.ToString.Format("{0:F4}", price)
但它實際上并沒有改變數字中的任何東西。在案例編號為 123456789 的情況下,我希望它顯示 123.456.789 我已經解決了一半,ToString("N2")但它也顯示了我不想要的 .00 小數。
uj5u.com熱心網友回復:
這里有一些小數和整數之間的特殊情況,因此需要以不同的方式處理它們。
Private Function formatWithTrailingZeros(number As Double) As String
If number Mod 1 > 0 Then ' has a fractional component
Return $"{number:0.00000000}"
Else
Dim formattedString = $"{number:N2}"
Return formattedString.Substring(0, formattedString.Length - 3)
End If
End Function
Dim price = 0.00002001
Console.WriteLine(formatWithTrailingZeros(price))
price = 0.00002
Console.WriteLine(formatWithTrailingZeros(price))
price = 123456789
Console.WriteLine(formatWithTrailingZeros(price))
price = 123456789.012345
Console.WriteLine(formatWithTrailingZeros(price))
0.00002001
0.00002000
123,456,789
123456789.01234500
如果您的第二種情況123.456.789不是基于您當前的文化,那么您可能需要,替換.為
Return formattedString.Substring(0, formattedString.Length - 3).Replace(",", ".")
由于您同時使用.小數分隔符和千位分隔符,我不確定我的示例123456789.012345000應該是什么樣子,但由于您沒有問,我不會猜測。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/428262.html
標籤:VB.net
上一篇:如何從存盤庫啟動前臺服務
