954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

String>Double Decimals not working

Hey, I'm making a math bot, and when you type in your equation it converts the numbers to doubles right away.

Except, when doing any equation with a decimal, the decimal is ignored.

Example

2 + 2 = 4 (No decimals, it works fine.)

2.2 + 2 = 24 (Decimal is ignored, 2.2 now equals 22.)

How would I be able to fix this?

NargalaX
Junior Poster in Training
69 posts since Aug 2009
Reputation Points: 18
Solved Threads: 1
 

What does that particular segment of your code look like (if you're able to post it)? MathBot sounds interesting -- combing netspace to solve random equations it encounters. ;)

jonsca
Quantitative Phrenologist
Team Colleague
5,621 posts since Sep 2009
Reputation Points: 1,165
Solved Threads: 581
 

This is a comma, point problem. If you use a comma as decimal point you probably don't have the problem. Depends on the standard international settings.

ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
 

You can also do culture-specific parsing of numbers:

private void button1_Click(object sender, EventArgs e)
    {
      CultureInfo ciEnglish = new CultureInfo("en-US");
      CultureInfo ciFrench = new CultureInfo("fr-FR");
      decimal dEnglish, dFrench;
      const string s = @"3.1415";
      if (decimal.TryParse(s, NumberStyles.Number, ciEnglish, out dEnglish))
        Console.WriteLine(string.Format("[en-US]: Parsed {0} to value: {1:F4}", s, dEnglish));
      else
        Console.WriteLine(string.Format("[en-US]: Failed to parse {0}", s));

      if (decimal.TryParse(s, NumberStyles.Number, ciFrench, out dFrench))
        Console.WriteLine(string.Format("[fr-FR]: Parsed {0} to value: {1:F4}", s, dFrench));
      else
        Console.WriteLine(string.Format("[fr-FR]: Failed to parse {0}", s));
    }
[en-US]: Parsed 3.1415 to value: 3.1415
[fr-FR]: Failed to parse 3.1415


Or you can set the culture information thread-wide for your main thread:

System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
sknake
Industrious Poster
4,954 posts since Feb 2009
Reputation Points: 1,764
Solved Threads: 735
 

Thanks everyone, but sknake, what namespaces must I acquire before using cultureinfo, because it is 'missing an assembly reference', and I am assuming this will work with double values aswell as decimals?

EDIT: I also got "The name 'NumberStyles' does not exist in the current context (CS0103)".


Also, how would I use regex to split a string on every occurence of "\ * - +" ?

Thanks for your help guys, I appreciate it.

NargalaX
Junior Poster in Training
69 posts since Aug 2009
Reputation Points: 18
Solved Threads: 1
 

In this case you can use the string Split method : http://msdn.microsoft.com/en-us/library/b873y76a.aspx
CultureInfo belongs to System.Globalization

ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
 

hi
i'm new to C# Programing & i found tat after declaring variables as double u may use
math.round(double value , number of decimal places).

jothythangaraj
Newbie Poster
1 post since Dec 2009
Reputation Points: 10
Solved Threads: 1
 

Thanks everyone, this works method works nicely!

NargalaX
Junior Poster in Training
69 posts since Aug 2009
Reputation Points: 18
Solved Threads: 1
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: