Link to home
Start Free TrialLog in
Avatar of roland4
roland4

asked on

Why are the data types double, decimal and float represented with a comma?

Hi

I´m searching since hours for the reason why double, decimal and float are represented with a comma in C#.
I tried anything to convert the values to work with PostgreSQL (that can´t work with commas), but I failed :-(

How can I combine these two worlds?
Avatar of farsight
farsight

double x;
x = 123456789.123456789;
System.Diagnostics.Debug.WriteLine(x);

prints:  123456789.123457
What commas?

How exactly is this not working for you with PostgreSQL?
Can you provide a snippet of the code that's not working?

Also, is this an international issues problem?  I.E. 123,456,789.123,457 vs. 123.456.789,123.457
Avatar of roland4

ASKER

For example I tried:

public class test
{
  private double tax = 0.1;
  private double runtime = 0.2;

  public test(decimal t, decimal r)  //this parameters come from updown-elements with the code this.updown1.value
  {
     tax = Convert.ToDouble(t);
     runtime = Convert.ToDouble(r);

     // Here I tried to echo the values with:
     MessageBox.Show(tax + " " + runtime);
  }
}

Unfortunately the MessageBox returns values like "0,9" not "0.9"...
ASKER CERTIFIED SOLUTION
Avatar of basetew
basetew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You know the internal representations of numbers are binary, not text, right?

Only the printed version has the comma.
runtime is automatically converted to a string representation in the expression:   tax + " " + runtime
That expression is equivalent to:   tax.ToString() + " " + runtime.ToString()

This is an "international issue".  You've got some Windows setting, or an international version of Windows, or something.  I'm right in the middle of U.S.A., and I've never had to deal with those issues.  I'm sure someone else here will be able to help with the specifics.

--- Below here is just guesses as to useful info. --- Don't let this lead you astray. --- (and let me know what works) ---

This is the first this you should check.
Apparently, here are the systemwide settings:
  On Window 2000, menu Start | Settings | Control Panel | Regional Options.
  On Windows XP, menu Start | Control Panel | Regional and Language Options.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemDoubleClassToStringTopic2.asp
"Converts the numeric value of this instance to its equivalent string representation using the specified culture-specific format information."
and see also the System.Globalization namespace.
System.Globalization.NumberFormatInfo      "Defines how numeric values are formatted and displayed, depending on the culture."

I use farsight's useful info advice already - to test that my code works in USA (uses . for decimals), as well as France (which uses , for decimals). Without InvariantCulture, my code breaks all over the place, mainly because databases and registry only deal with string. ToString() is errornous, I fix my problems cleanly by adding InvariantCulture as ToString's overload.
Very cool.  We both learned something thanks to basetew.
Give him the points.
Avatar of roland4

ASKER

Thanks to both of you. Now it functions!