简体   繁体   English

十进制千位分隔符和小数点

[英]Decimal Thousand Separator and Decimal Point

I googled the web for hours but couldn't find my answer. 我在网上搜索了几个小时,但找不到答案。 I need when a user types or copies any number in TextBox then the number should follow the bellow rules: 当用户在TextBox中键入或复制任何数字时,我需要该数字应遵循以下规则:

  1. Only one decimal point should be permitted. 只能允许小数点后一位。
  2. Thousand Separator. 千位分隔符。
  3. Trailing zeros should be removed. 尾随零应删除。 I mean the zeros after decimal point should NOT be displayed. 我的意思是不应显示小数点后的零。

Update For example, 更新例如

123 => 123 123 => 123

1234.00 => 1,234 1234.00 => 1,234

123456.05 => 123,456.05 123456.05 => 123,456.05

123456.50 => 123,456.5 123456.50 => 123,456.5

How can I do this? 我怎样才能做到这一点?

The easiest way would be to try to parse value as a number. 最简单的方法是尝试将值解析为数字。

decimal value;
if(decimal.TryParse(textBox.Text, out value)
{
   //value is ok
   textBox.Text = string.Format("{0}", value);//If you want some fancier formatting
}
else
{
    //Value is not valid
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TemplateField小数点和千位分隔符 - TemplateField decimal places and thousand separator 当千位分隔符和十进制分隔符是相同的C#时进行十进制分析 - Decimal parse when the thousand separator and decimal separator are the same C# 解析后十进制不显示组(千)分隔符 - Decimal not showing group(thousand) separator after parse 更改绑定中的默认千位和小数分隔符 - Changing the default thousand and decimal separator in a binding 将double转换为带有N个小数的字符串,点作为小数点分隔符,没有千位分隔符 - Converting double to string with N decimals, dot as decimal separator, and no thousand separator 与decimal.tryparse的C#千位分隔符问题 - C# thousand separator issue with decimal.tryparse 给定IFormatProvider的千位分隔符就像小数点后一位 - Apply thousand separator given the IFormatProvider just like decimal one 使用千位分隔符将十进制转换为字符串的简单方法 - Easy way to parse decimal to string with Thousand's Separator 如何在本地化的千位分隔符中格式化数字以保持小数位相同? - How to format a number in localized thousand separator keeping the decimal digits the same? 如何在.net中获取当前语言环境的默认十进制/千位分隔符 - How to get default decimal / thousand separator for current locale in .net
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM