转换成字符串值与千分十进制? [英] Convert string value to decimal with thousand separator?

查看:122
本文介绍了转换成字符串值与千分十进制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个很简单的问题。
假设文本框中显示值10,000.12当用户编辑数据被错误删除,他前两个数字像,000.12并使用该文本框在计算则给出了一个例外。我要一个公正的验证文本框

This might be a very simple question. Suppose text box show value 10,000.12 when user edits data by mistake he remove first two numbers like ,000.12 and using this textbox in the calculation then it gives an exception. I want a just validate text box.

例如:
字符串str =100.12,
转换为
十进制数= 100.12;

For Example: string str = ",100.12;" Convert to decimal number = 100.12;

任何想法

它?显示当用户删除任何千位分隔符仅整数。

It shows only whole number when user remove any thousand separator.

推荐答案

如果我明白的问题,你想删除将所有字符。防止解析程序从失败

If I understood the question, you want to remove all characters that would prevent the parse routine from failing.

string str = ",0,100.12";

var modified = new StringBuilder();
foreach (char c in str)
{
    if (Char.IsDigit(c) || c == '.')
        modified.Append(c);
}

decimal number= decimal.Parse(modified.ToString());

这篇关于转换成字符串值与千分十进制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆