用各种十进制/基数分隔符解析小数的方法 [英] Method to parse decimals with various decimal/radix separators

查看:73
本文介绍了用各种十进制/基数分隔符解析小数的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了很多文件解析,其中涉及解析数据类型(例如十进制).为了使代码更具可读性,我一直在使用以下方法:

I do a lot of file parsing, which involves parsing data types like decimal. To help make the code more readable, I've been using the following method:

public static decimal? ToDecimal(this string data)
{
    decimal result;
    if (decimal.TryParse(data, NumberStyles.Integer | NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out result))
        return result;

    return null;
}

这对于以小数点分隔符(以小数点/句点表示)的十进制效果很好.但是,我希望此函数可与其他标准的十进制分隔符一起使用,尤其是逗号.(我读到还有一个阿拉伯数字分隔符: http://en.wikipedia.org/wiki/Decimal_mark#Other_numeral_systems ,但这大概也取决于能够解析东部阿拉伯数字).

This works fine for decimals which are represented with a full-stop/period as the decimal separator. However, I'd like this function to work with other standard decimal separators, particularly the comma. (I read that there is also an Arabic decimal separator: http://en.wikipedia.org/wiki/Decimal_mark#Other_numeral_systems, but that presumably relies on being able to parse the eastern arabic numerals too).

Culture.CurrentCulture不合适,因为不一定必须在执行处理的计算机上创建数据.所以我现在有了这个:

The Culture.CurrentCulture wouldn't be appropriate because the data is not necessarily created on the machine that is doing the processing. So I've now got this:

private static CultureInfo CreateCultureWithNumericDecimalSeparator(string separator)
{
    var cultureInfo = (CultureInfo)CultureInfo.InvariantCulture.Clone();
    cultureInfo.NumberFormat.NumberDecimalSeparator = separator;
    return cultureInfo;
}

private static CultureInfo[] cultureInfos = new CultureInfo[]
{
    CultureInfo.InvariantCulture,
    CreateCultureWithNumericDecimalSeparator(",") // Normal comma
};

public static decimal? ToDecimal(this string data)
{
    foreach (CultureInfo cultureInfo in cultureInfos)
    {
        decimal result;
        if (decimal.TryParse(data, NumberStyles.Integer | NumberStyles.AllowDecimalPoint, cultureInfo, out result))
            return result;
    }

    return null;
}

这可行,但是解析两次,尤其是考虑到TryParse正在检查各种设置(千位分隔符,十六进制说明符,货币符号,指数等)时,显得有些沉重.这可能不是性能问题,但是我很想知道是否有更有效的方法来执行此操作,或者甚至框架中是否存在现有方法?甚至还有一种方法可以应付现代使用的其他数字系统?谢谢.

This works, but parsing twice, especially given that TryParse is checking all sorts of settings (thousand separators, hex specifiers, currency symbols, exponents, etc) seems a little heavy. It's probably not a performance issue, but I'm curious to know if there's a more efficient method to do this, or possibly even an existing method within the framework? And maybe even a method that could cope with other numeral systems in modern use? Thanks.

推荐答案

似乎该框架没有直接提供此功能.有趣的是,答案另一个问题建议,即使使用文化集,该框架也没有提供任何用于解析阿拉伯东部数字的机制.由于东部阿拉伯数字要求对我的应用程序来说只是理论上的问题,因此我坚持使用上面已经讲过的代码.如果我实现更具体的功能,我将其发布!

It seems that that the framework doesn't provide this directly. Interestingly, answers to another question suggest that the framework doesn't provide any mechanism for parsing Eastern Arabic numerals, even with the culture set. As the Eastern Arabic numerals requirement was just theoretical for my app, I'm sticking with the code I've already got above. If I implement anything more specific, I'll post it!

这篇关于用各种十进制/基数分隔符解析小数的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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