Question why format changing ?

andrews

Well-known member
Joined
Nov 22, 2011
Messages
132
Programming Experience
5-10
Why do I receive another format and how can I make it good?

Dim a As Double = 5.126
Dim w As String
w = CStr(a) ' w = 5,126 not good

I have to get w = 5.126
Thanks for any response
 
In code, literals always use a comma (,) as a group separator and a dot (.) as a decimal separator so that the same code will always compile the same way whenever and wherever that code is compiled. That means that 5.126 always means five units, one tenth, two hundredths and six thousandths no matter what the regional settings are on your machine. If you want five thousand, one hundred and twenty six then you need to use 5,126 or just 5126, regardless of your regional settings. In short, code uses US format for literals no matter what. It's just like a Date literal is ALWAYS written in #M/dd/yyyy# even if your regional settings indicate that dates use d/MM/yyyy format.

When you run your application though, what you see as a String representation of numbers, dates, etc, DOES depend on your regional settings. If your current culture specifies that the comma is the decimal separator and the dot is the group separator then that's what you will see when you convert a number to a String without specifying your own format and vice versa.

So, if you want to specify the number five thousand, one hundred and twenty six then either omit all separators or use a comma as that is what a literal uses as a group separator.

If this doesn't answer your question, please explain what your current regional settings are and what you actually expect to happen in words rather than in numbers, which are ambiguous in this context. if they weren't then you wouldn't have an issue in the first place. Please ALWAYS provide a FULL and CLEAR explanation. The more we have to guess, the less likely you are to get the help you want.
 
Question why format changing ?

My regional settings on my computer are Belgian,thus "," is a decimal setting.
In my programs in vb.net I use always "." as a decimal setting and there are never problems in the programs.
But now I have to get results outside my program in string format in a txt file where "." must be "." but when I convert decimal numbers in string format I get ",".
Can I change the settings in one proram so that it is working in another regional setting or is there another solution?
 
Whenever you convert a number to a String, it will use the current system regional settings by default. As suggested, using ToString or String.Format will give you the opportunity to specify an IFormatProvider that contains the desired format information. In your case, you might like to use NumberFormatInfo.InvariantInfo or CultureInfo.InvariantCulture, which will give you the format you want where separators are concerned.
 
Question why format changing ?

It seems to complicated to me to find a solution in the way you write
But I found now a solution who is simple.
Replace(double object.ToString, ",", ".")
Thanks any way for helping me.
 
Complicated? I don't think you actually tried very hard in that case. What's complicated about:
w = a.ToString(Globalization.NumberFormatInfo.InvariantInfo)

It was a rhetorical question. There's nothing complicated about that.
 
Back
Top