-5

I want to parse my string to double. My problem is that the result of newTime is 800, but the result should be 8,00.

string time = "08:00";
double newTime = double.Parse(time.Replace(':', '.'));
A. H
  • 47
  • 1
  • 9
  • 2
    `8,00` isn't a valid double – Liam Feb 20 '17 at 10:58
  • 2
    I get `8` when I execute your code – David Pilkington Feb 20 '17 at 10:58
  • 1
    what are you trying to achieve? Calculate number of hours from HH:mm format? – madoxdev Feb 20 '17 at 10:58
  • 3
    Times aren't doubles. It's very unclear what you're asking here... – Jon Skeet Feb 20 '17 at 10:59
  • But I want to get 8,00 with two decimalpoints – A. H Feb 20 '17 at 11:01
  • 1
    I suppose your culture has , as decimal separator. So try to parse like thi: double newTime = double.Parse(time.Replace(':', '.'),CultureInfo.InvariantCulture); – Kodre Feb 20 '17 at 11:01
  • I think that the problem is with styling output. You should use string formatting like **[this](http://stackoverflow.com/questions/6951335/using-string-format-to-show-decimal-upto-2-places-or-simple-integer)** – Piotr Kamoda Feb 20 '17 at 11:02
  • I have try to parse with CultureInfo.InvariantCulture, but it brings nothing, the result is still 8. – A. H Feb 20 '17 at 11:03
  • @A.H yes that is correct it cannot be nothing else as you have double not string. What would you like to get string without leading zero and , insetead of : ? – Kodre Feb 20 '17 at 11:04
  • 1
    @A.H Just listen to all these people instead of saying err derr result is still 8. Its a double 8,0 = 8 period. Would you write "Do that 5 times" -> "Do that 5,0 times"... No you don't. The way you wan't to format you just need a string, Piotr Kamoda has the perfect answer for that in his comment – EpicKip Feb 20 '17 at 11:09
  • Thanks @Piotr Kamoda – A. H Feb 20 '17 at 11:11
  • If it is a time, why don't you just parse into a TimeSpan object? ie TimeSpan.Parse("08:00") – Andez Feb 20 '17 at 11:27
  • @A.H ok now i understand what you want, so it is probably the best to do it like this time.Replace(':', ',').TrimStart('0') – Kodre Feb 20 '17 at 11:32

4 Answers4

1

If you want to treat : as a decimal separator, just do it:

  string time = "08:00";

  // when parsing "time", decimal separator is ":"
  double newTime = double.Parse(time, 
    new NumberFormatInfo() { NumberDecimalSeparator = ":" });

Try avoiding tricks with magic constants like '.' in the time.Replace(':', '.'). Please, notice that newTime will be 8, not 8.00 (since 8 == 8.0 == 8.00 == 8.000...). If you want to represent newTime with two digits after the decimal point use formatting:

  // F2 - format string ensures 2 digits after the decimal point
  // Outcome: 8.00
  Console.Write(newTime.ToString("F2"));   
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
0

The result of Double.Parse is a Double, not a string. You need to output a string from the double, using ToString.

You should also use an overload of Double.Parse that has a NumberStyles parameter. Using the Float value allows exponent notation.

Fabol Lous
  • 34
  • 7
0
string time = "08:00";
double newTime = double.Parse(time.Replace(':', '.'),  CultureInfo.InvariantCulture);
Toshi
  • 2,532
  • 4
  • 17
  • 45
0

Your issue is that your culture has a different decimal separator to what you are creating in your string.

You can change it to to this

string time = "08:00";
double newTime = double.Parse(time.Replace(":", Thread.CurrentThread.CurrentCulture.NumberFormat.NumberDecimalSeparator) );
David Pilkington
  • 13,528
  • 3
  • 41
  • 73