怎样才可以有Decimal.TryParse解析0.0? [英] How can I have Decimal.TryParse parse 0.0?

查看:174
本文介绍了怎样才可以有Decimal.TryParse解析0.0?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法让Decimal.TryParse解析的0.0或00.00或000.000的字符串值,0?

我已经尝试设置的NumberStyles以任何。

解决方案
  

有没有办法让Decimal.TryParse解析字符串值0.000.00000.000为0?

我是跨preting你的问题的意思。说我拿串0.000.00000.000要求 Decimal.TryParse 解析他们。当我写出来所产生的小数点控制台我看到 0.0 0.00 0.000 分别。有没有办法让 Decimal.TryParse 返回十进制在所有这些案件将被写入到控制台为 0

没有。想想为什么这应该是。 十进制类型重新present precise号码;在某些圈子里, 0.00 将被视为更precise比 0.0 这将被视为更precise比 0 。如果 Decimal.TryParse 截断了precision比十进制键入不会为这些目的。

这就是说,它很容易,只是修剪尾随零调用解析之前:

 静态的char [] whitespaceAndZero =新的[] {
    '',
    \ t,
    '\ r',
    '\ N',
    '\ u000b',//垂直制表
    '\ u000c',//换
    '0'
};
静态的字符串TrimEndWhitespaceAndZeros(字符串s){
    返回s.Contains('。')? s.TrimEnd(whitespaceAndZero):S;
}

静态布尔TryParseAfterTrim(字符串s,出小数D){
    返回Decimal.TryParse(TrimEndWhiteSpaceAndZeros(S),输出D);
}
 

用法:

 字符串s =0.00;
十进制D组;
TryParseAfterTrim(S,出D);
Console.WriteLine(四);
 

输出:

  0
 

请注意,上面只显示了如何解决你的问题的症结所在。它是由你来决定是否和你将如何处理本地化问题。至少,把这个投入生产之前,你应该考虑更换硬codeD CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator 。你应该考虑具有 TryParseAfterTrim 过载使用相同的参数列表 Decimal.TryParse 。那就是:

 布尔TryParseAfterTrim(
    字符串s,
    NumberStyle风格,
    的IFormatProvider提供商,
    出小数结果
)
 

Is there a way to get Decimal.TryParse to parse a string value of "0.0" or "00.00" or "000.000" as 0?

I have tried setting NumberStyles to Any.

解决方案

Is there a way to get Decimal.TryParse to parse a string value of "0.0" or "00.00" or "000.000" as 0?

I am interpreting your question to mean. Say I take the strings "0.0", "00.00" and "000.000" ask Decimal.TryParse to parse them. When I write out the resulting decimal to the console I see 0.0, 0.00 and 0.000 respectively. Is there a way to get Decimal.TryParse to return a decimal in all these cases that will be written to the console as 0?

No. Think about why this should be. Decimal types represent precise numbers; in certain circles, 0.00 would be considered more precise than 0.0 which would be considered more precise than 0. If Decimal.TryParse truncated that precision than the Decimal type would not be useful for these purposes.

That said, it's easy enough to just trim the trailing zeros before calling parse:

static char[] whitespaceAndZero = new[] {
    ' ',
    '\t',
    '\r',
    '\n',
    '\u000b', // vertical tab
    '\u000c', // form feed
    '0'
};
static string TrimEndWhitespaceAndZeros(string s) {
    return s.Contains('.') ? s.TrimEnd(whitespaceAndZero) : s;
}

static bool TryParseAfterTrim(string s, out decimal d) {
    return Decimal.TryParse(TrimEndWhiteSpaceAndZeros(s), out d);
}

Usage:

string s = "0.00";
decimal d;
TryParseAfterTrim(s, out d);
Console.WriteLine(d);

Output:

0

Please note that the above only shows the crux of how to solve your problem. It is up to you to decide whether or not and how you are going to handle localization issues. At a minimum, before putting this into production you should consider replacing the hard-coded '.' with CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator. You should consider having an overload of TryParseAfterTrim with the same parameter list as Decimal.TryParse. That is:

bool TryParseAfterTrim(
    string s,
    NumberStyle style,
    IFormatProvider provider,
    out decimal result
)

这篇关于怎样才可以有Decimal.TryParse解析0.0?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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