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?

## All 7 Replies

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

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.

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``````

``System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");``

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.

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

Thanks everyone, this works method works nicely!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.