6

In C# visual studio not taking decimal points in double after read and converted into double using

Convert.ToDouble(Console.ReadLine()); 

command... For example if 12.25 is typed it saves the value as 1225. Can I get any help? This is the Code i'm using.

double number;
Console.WriteLine("Enter a number with a two decimal places!");
number = Convert.ToDouble(Console.ReadLine());
Console.WriteLine(number):
Console.ReadLine();
RoadieRich
  • 6,330
  • 3
  • 35
  • 52
Moorish Awan
  • 193
  • 2
  • 12
  • 2
    Can you show the code because what you describe should be working correctly. – Michael Z. May 26 '16 at 13:14
  • You might have the wrong culture, it might be looking for `,` instead of `.` as a seperator. (Also please change your title to a more descriptive one) – counterflux May 26 '16 at 13:16
  • Welcome to SO. Please read [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). – 001 May 26 '16 at 13:18
  • I've shown the code now..! – Moorish Awan May 26 '16 at 13:31
  • And sorry for weak phrased question as i'm new to this..! – Moorish Awan May 26 '16 at 13:32
  • @AbdulRaheem have you tried converting it to int and then converting the int to a double? – David May 26 '16 at 13:33
  • I just tested it [here](https://ideone.com/xRizms) and if it is a Culture issue, an exception is thrown. But it doesn't look like you are getting an exception, are you? (Also, the 3rd line of code has a capital 'N'. Is that a typo?) – 001 May 26 '16 at 13:46
  • that was a typo i've corrected it and i thought if i convert the input into int first it would omit the decimal places and round off the value so i was tried directly converting input string to double.P.SI wrote the code on a differnet computer it worked but when i opened the same code on my laptop it wasn't working so i'm thinking probably there's something fishy about culture on my laptop. – Moorish Awan May 26 '16 at 13:49
  • Possible duplicate of [how to convert string to double with proper cultureinfo](http://stackoverflow.com/questions/2583362/how-to-convert-string-to-double-with-proper-cultureinfo) – Petter Friberg Jun 03 '16 at 19:34

3 Answers3

10

Your current code should be working as expected and if it isn't, it's likely the result of cultural issues that expect your double string to be in a different format (i.e. 12,25 instead of 12.25).

Applying the Current Culture When Parsing

You can handle this issue by applying the current culture when parsing your value within the Convert.ToDouble() method through the System.Globalization namespace:

// This would use the current culture to handle reading your value
double input = Convert.ToDouble(Console.ReadLine(),CultureInfo.InvariantCulture);

Handling Various Cultures (i.e. periods or commas)

If you wanted to expect to handle either periods or commas, you could potentially perform a replacement the in the same manner :

// Get the number separator for this culture and replace any others with it
var separator = CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator;
// Replace any periods or commas with the current culture separator and parse
var input = Double.Parse(Regex.Replace(Console.ReadLine(),"[.,]",separator));

You can see a working example of this here.

Rion Williams
  • 74,820
  • 37
  • 200
  • 327
6

When you local culture doesn't use . as a decimal delimiter it won't work. If you want to enter doubles with a . decimal separator, you need to specify a culture that does use it.

Convert.ToDouble(yourString, System.Globalization.CultureInfo.InvariantCulture)

If you want to output it in the same format, use the same CultureInfo again:

CultureInfo culture = CultureInfo.InvariantCulture;
double input = Convert.ToDouble(Console.ReadLine(), culture);
Console.WriteLine(String.Format(culture, "{0}", input));
Manfred Radlwimmer
  • 13,257
  • 13
  • 53
  • 62
0

It could be a globalization issue as some cultures use , instead of . to separate decimal places. Hopefully it will solve your problem.

string userInput = Console.ReadLine();
double inputInDouble = Convert.ToDouble(userInput,System.Globalization.CultureInfo.InvariantCulture);
Asad
  • 498
  • 7
  • 21