[英]Format a decimal without comma
i try to format a decimal as 0:0.0 in c# tried this code我尝试在 c# 中将十进制格式设置为 0:0.0 尝试了此代码
string nv = textBox7.Text.Trim().Replace(',', '.');
res = Convert.ToDecimal(nv, new CultureInfo("en-GB"));
but res always show a result with a comma, tried also with但 res 总是用逗号显示结果,也尝试过
new CultureInfo("en-GB")
but the problem persist And thank you in advance.但问题仍然存在 并提前感谢您。
As other comments and answers suggest, you have some basics to understand first.正如其他评论和答案所暗示的那样,您首先要了解一些基础知识。 I may be saying some things you already know, but bear with me:我可能会说一些你已经知道的事情,但请耐心等待:
textBox7.Text
contains a string
, not a decimal
.您的textBox7.Text
包含一个string
,而不是一个decimal
。decimal
for calculations, you have to convert it (I think you already got this far)如果你想要一个decimal
进行计算,你必须转换它(我认为你已经做到了这一点)res
is a decimal, whenever you want to look at its value SOMETHING will convert it to a string
.由于res
是一个十进制数,因此无论何时您想查看其值,SOMETHING 都会将其转换为string
。 Whether that's you writing it to the Console
or your debugger when you mouse over it.当您将鼠标悬停在它上面时,无论是将其写入Console
还是调试器。 That conversion will use your current Regional Settings.该转换将使用您当前的区域设置。 This is why you always see a comma.这就是为什么你总是看到一个逗号。 a) Standard Format . a) 标准格式。 Example:例子:
Console.WriteLine(res.toString("F2"));
This will format 123456 with 2 numbers after the comma: 123456.00这将用逗号后的 2 个数字格式化 123456:123456.00
b) Custom Format . b) 自定义格式。 Example:例子:
Console.WriteLine(res.toString("[##-##-##]"));
This will output 123456 to something like [12-34-56]这将输出 123456 到类似 [12-34-56]
c) CultureInfo . c) 文化信息。 Example:例子:
Console.WriteLine(res.ToString(CultureInfo.CreateSpecificCulture("nl-BE")));
This will output 1234.56 like in Belgium: with a comma 1234,56这将像在比利时一样输出 1234.56:带逗号 1234,56
Incidentally, I think en-GB also outputs to a comma :-)顺便说一句,我认为 en-GB 也输出到逗号 :-)
d) Combine . d) 结合。 Go nuts!发疯! Do both !两者都做! Example:例子:
Console.WriteLine(res.ToString("F2", CultureInfo.CreateSpecificCulture("nl-BE")));
formats 123456 to 123456,00 !格式 123456 到 123456,00 !
res
is a decimal
, not a string. res
是一个decimal
,而不是一个字符串。 So it can't have a format.所以它不能有格式。 Decimals are pure mathematical numbers without an associated format.小数是纯数学数字,没有相关格式。 The format only comes into existence when you convert a decimal
to a string
.该格式仅在您将decimal
转换为string
时才存在。
You can use res.ToString(CultureInfo.InvariantCulture)
to produce a string that uses .
您可以使用res.ToString(CultureInfo.InvariantCulture)
生成使用.
as decimal separator.作为小数点分隔符。
这将为您提供没有逗号的十进制数字,例如“15000.00”:
FormatNumber(nv, , , , 0)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.