Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8392425
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T19:31:02+00:00 2026-06-09T19:31:02+00:00

I do a lot of file parsing, which involves parsing data types like decimal.

  • 0

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;
}

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).

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;
}

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T19:31:04+00:00Added an answer on June 9, 2026 at 7:31 pm

    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!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a program in C++ which involves parsing a lot of text
I found a library called Assimp for parsing a lot of 3D model file
My file has a lot of line breaks, like this: This is a line.
i have a file which contains a lot of update query and some other
I am writing a lot of data to a file, and while keeping my
I have a large JSON file that contains A LOT of similar code. It's
I try to generate a csv file through a lot of functions like that
I have a big xml file and parsing it consumes a lot of memory.
I am working on a project which basically involves parsing and storing information about
I'm working on application which has workflow like this: 1.parsing home page (using HttpURLConnection,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.