0

How to convert in C# a double with 1,77 to 1.77?

I have a input text with 1,77 and I want to replace to 1.77.

I have tried

alturaaux =Convert.ToDouble(altura2).ToString(#,##);

but without success.

Tiny
  • 27,221
  • 105
  • 339
  • 599
Ricardo Diz
  • 31
  • 1
  • 8
  • this question can help you http://stackoverflow.com/questions/4076789/converting-double-to-string-with-n-decimals-dot-as-decimal-separator-and-no-th – jandresrodriguez Nov 01 '14 at 13:21
  • 1
    Why convert to double? Use the native String.replace method: http://msdn.microsoft.com/en-us/library/czx8s9ts(v=vs.110).aspx. You don't have a *double* to begin with. – Jongware Nov 01 '14 at 13:33
  • Why are you getting 1,77? Is it a culture thing (i.e. many European cultures use `,` as the decimal separator). If you are storing something that is really a decimal as a string, maybe you should look into converting it at storage time when you are in the correct culture. – John Koerner Nov 01 '14 at 13:40

2 Answers2

2

You can first get it to a string than replace the comma to a dot.

string entered = "1,77";
string doubleString = entered.Replace(',', '.');

if(Double.TryParse(doubleString, out number))
    return number;
else 
   return null;
  • +1 for the correct suggestion, though all this can be done in a single line. – Rahul Nov 01 '14 at 13:51
  • He said he is going to have the user input their region currency, so I broke it down enough for him to understand, plus I believe he is cheating on homework so better make him look at more code than needed. – Katy Pillman Nov 01 '14 at 13:53
0

use TryParse method like

double ret;
double.TryParse("1,77".Replace(",", "."), out ret);
Rahul
  • 76,197
  • 13
  • 71
  • 125