转换负十进制字符串丢失 - [英] Converting Negative Decimal To String Loses the -
问题描述
我需要负myDecimalValue.ToString(C)发送;问题是如果myDecimalValue为负,可以说-39,我越来越$ 39.00为字符串不是$ 39.00转换后。所以,我不知道如何去这一点。
I'm required to send in a negative myDecimalValue.ToString("C"); The problem is if myDecimalValue is a negative, lets say -39, after conversion I'm getting $39.00 as the string not $39.00. So I'm not sure how to go about this.
这是工具方法,发生在小数。如果小数点是负的,我要的ToString显示负
This is the utility method that takes in the decimal. If the decimal is negative, I want the ToString to show a negative
public static BasicAmountType CreateBasicAmount(string amount, CurrencyCodeType currencyType)
{
BasicAmountType basicAmount = new BasicAmountType
{
currencyID = currencyType,
Value = amount
};
return basicAmount;
}
我可能会有两种方式,C或F2,我才不管是关于得到那个负号施行本科生字符串,如果输入十进制为负。我想,有没有办法做到这一点,除非我检查我的消极实用方法里面在这里。我不能只是给一个负数,并期望的ToString工作,为的ToString自动看到小数点为负传入?
I could go either way, a C or F2, all I care is about getting that negative sign intothe string if the incoming decimal is negative. I suppose there's no way to do this unless I check for negativity inside my utility method here. I can't just send a negative number and expect the ToString to work and for the ToSTring to automatically see that the decimal is negative incoming?
推荐答案
您可以试试这个:
decimal myDecimalValue = -39m;
string s = String.Format("${0:0.00}", myDecimalValue); // $-39.00
不过,SLaks是正确的,负值通常在括号中。
However, SLaks is right, negative values are usually shown in parenthesis.
这篇关于转换负十进制字符串丢失 - 的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!