[英]String to Decimal with 2 decimal places always
I have a string that is to be converted to decimal. 我有一个要转换为十进制的字符串。 The string could be entered with no decimal places eg "123" or 2 decimal places, eg "123.45" or somewhat awkwardly, 1 decimal place "123.3". 可以输入的字符串不带小数位,例如“ 123”或2个小数位,例如“ 123.45”或有点尴尬的1个小数位“ 123.3”。 I want the number displayed (the Property invoice.Amount
which is type decimal
) with 2 decimal places. 我想要显示2个小数位的数字(Property invoice.Amount
,类型为decimal
)。 The code below does that. 下面的代码可以做到这一点。 I think it could be written better though. 我认为可以写得更好。 How? 怎么样?
decimal newDecimal;
bool isDecimal = Decimal.TryParse(InvoiceDialog.InvoiceAmount, out newDecimal);
string twoDecimalPlaces = newDecimal.ToString("########.00");
invoice.Amount = Convert.ToDecimal(twoDecimalPlaces);
In part, I don't understand, for the string formatting "########.00", what # does and what 0 does. 对于字符串格式为“ ########。00”的部分内容,我不了解,#的含义和0的含义。 Eg how would it be different if it were "########.##"? 例如,如果是“ #########。##”,会有什么不同?
#
is an optional digit when 0
is a mandatory digit #
是可选数字, 0
是必填数字
For instance 例如
decimal d = 12.3M;
// d with exactly 2 digits after decimal point
Console.WriteLine(d.ToString("########.00"));
// d with at most 2 digits after decimal point
Console.WriteLine(d.ToString("########.##"));
Outcome: 结果:
12.30 // exactly 2 digits after decimal point: fractional part padded by 0
12.3 // at most 2 digits after decimal point: one digit - 3 - is enough
Basically, # means optional, where as 0 is mandatory. 基本上,#表示可选,其中0为必填。 As for better explanation, if you put # then if number is available to fullfil the placeholder it'll be added if not it'll be ignored. 作为更好的解释,如果您输入#,则如果数字可用于填充占位符,则将其添加,否则将被忽略。
Putting 0 however is different as it'll always put a value in for you. 但是,将0设为不同,因为它将始终为您输入一个值。
You can combine the two together. 您可以将两者结合在一起。
String.Format("{0:0.##}", 222.222222);
// 222.22 // 222.22
String.Format("{0:0.##}", 222.2);
// 222.2 // 222.2
String.Format("{0:0.0#}", 222.2)
// 222.2 String.Format("{0:0.0#}", 222.2)
// 222.2
The "#" is optional while the "0" will show either the number or 0. “#”是可选的,而“ 0”将显示数字或0。
For example, 例如,
var x = 5.67;
x.ToString("##.###"); // 5.67
x.ToString("00.000"); // 05.670
x.ToString("##.##0"); // 5.670
If you just care about how many decimal places you have, I would recommend using 如果您只是在乎小数位数,建议您使用
x.ToString("f2"); // 5.67
to get 2 decimal spots. 得到2个小数点
More information can be found at http://www.independent-software.com/net-string-formatting-in-csharp-cheat-sheet.html/ . 可以在http://www.independent-software.com/net-string-formatting-in-csharp-cheat-sheet.html/中找到更多信息。
You don't need to convert the decimal
to string
to do the formatting for 2 decimal places. 您无需将decimal
转换为string
即可格式化2个小数位。 You can use the decimal.Round
method directly. 您可以直接使用decimal.Round
方法。 You can read about it here . 你可以在这里阅读。
So your code can be converted to 这样您的代码可以转换为
decimal newDecimal;
Decimal.TryParse(s, out newDecimal);
newDecimal = decimal.Round(newDecimal, 2, MidpointRounding.AwayFromZero);
The above code also be simplified with C# 7.0 declaration expression as 上面的代码也使用C#7.0声明表达式简化为
Decimal.TryParse(s, out decimal newDecimal);
newDecimal = decimal.Round(newDecimal, 2, MidpointRounding.AwayFromZero);
Now newDecimal will have have a value with 2 precision. 现在newDecimal的值将具有2个精度。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.