2009-07-24 47 views
2

我有需要格式化十进制数字作为货币,但我不希望任何舍入过程中发生。格式货币没有舍入

例如(例如文化是EN-US)

Dim money = 1234.556789D 
money.ToString("C") ' Yields $1,234.56 (notice the rounding & truncating) 

money.ToString("C99") ' Yields $1,234.556789000000000000....0 -- close but not perfect, those trailing zeros are unwanted. 

' I want something that would result in this... 
money.ToString("??") ' Yields $1,234.56789 

' likewise.... 
money = 0.1D 
money.ToString("??") ' Yields $0.10 (notice that it at least matches the culture's currency format -- two decimal places) 

假设所有这些应用程序的用户将使用EN_US规则我能有这样的“?”是硬编码的,如“$#,## 0.00 ##################################### ###########################“ - 但是这会让我的胃流失。有没有内置的方式来完成我后?

+1

我已经浏览了大约20分钟的帮助文档,但什么都没发现。抱歉。 – Greg 2009-07-24 21:54:04

+0

我很感谢你的时间格雷格。对不起,搜索离开了你(和我)空手。我想这样的需求并不是非常“正常”,这意味着框架不会自然地运用这个功能。哦,如下所述,我结束了我自己的解决方案,直到微软认为它将非常棒/足够用于烧入框架;) – ckittel 2009-07-26 19:12:11

回答

1

眼见似乎没有成为一个内置这样做的这样,我结束了滚滚,看起来像我自己的扩展方法...

Public Function ToUnroundedCurrency(ByVal value As Decimal) As String 
    Dim valueAsString As String = value.ToString() ' Some loss of precision happens here, but it is not a concern. 

    Dim decimalLocation As Integer = valueAsString.IndexOf(".") 

    ' Get number of digits after the decimal place in the number 
    Dim numberOfDecimalPlaces As Integer = 0 
    If (decimalLocation > 0) Then numberOfDecimalPlaces = valueAsString.Length - decimalLocation - 1 

    ' The currency formatter has a limit of 99 decimal places -- due to the decimal -> ToString() conversion above, this will never happen, but being defensive anyway. 
    If (numberOfDecimalPlaces > 99) Then 
     numberOfDecimalPlaces = 99 
    ElseIf (numberOfDecimalPlaces < CultureInfo.CurrentCulture.NumberFormat.CurrencyDecimalDigits) Then 
     ' also make sure we at least have the minimum required decimal places 
     numberOfDecimalPlaces = CultureInfo.CurrentCulture.NumberFormat.CurrencyDecimalDigits 
    End If 

    Return value.ToString("C" & numberOfDecimalPlaces) 
End Function 

我注意到一些可以忽略不计的损失的精度。我们(或者任何人)可能不会处理足够小的十进制值,以至于无法进入限制。

0

是的。下面是从另一个论坛回答:

http://forums.asp.net/p/1266337/2380486.aspx

+0

如果你的意思是做类似... Dim money = 1234.556789D Dim c = New Globalization.CultureInfo(System.Globalization.CultureInfo.CurrentCulture.Name) c.NumberFormat.CurrencyDecimalDigits = 99 x.ToString(“C”,c)0​​ 然后我得到所有未填充的十进制的尾随零数字。这是你在暗示的 - 还是我误解了你发给我的链接? – ckittel 2009-07-24 21:03:05

1

的ToString( “C20”) - 用C采用精密后缀

编辑:哎呀,没有明显读取的问题。

的ToString(“C20”)。TRIM(“0”)好像修复,但是当你使用的String.format这行不通......

+1

我想他只是想显示相关的数字。此*始终*显示20位数字。 – Thorarin 2009-07-24 20:58:42

2

你可以做这样的事情...

var money = 1234.556789D; 
Console.WriteLine(money.ToString(GetFormat(money))); 

money = .1D; 

Console.WriteLine(money.ToString(GetFormat(money))); 

使用下面的方法来获取格式字符串...

static string GetFormat(double input) 
{ 
    // get the number of decimal places to show 
    int length = input.ToString().Length - input.ToString().IndexOf(".") - 1; 

    // return the currency format string to use with decimal.ToString() 
    return string.Format("C{0}", length < 2 ? 2 : length); 
} 

如果你想采取了一步,你也可以这一切包装成一个扩展方法所以你不必打电话ToString()中的GetFormat()方法 - 可能会让事情看起来更清晰一些。

0

你可以尝试写一个正则表达式来摆脱所有那些尾随0。