Setting decimal symbol

J

Joza

Hi everybody!

I have question about setting decimal symbols... In some countries,
for example in my country we use comma for decimal symbol, but
in in some other countries it is decimal point. The problem is when
I use comma and want to save data in database.
For example if i want to save number 4,50 it saves like 450, but
if i write 4.50 it regulary saves the number in that format.
My question is how to make to program knows what to act if my app
is working on system where decimal symbol is comma or decimal point.
Or should i use only comma and when saving data i just parse the
number and replace comma with point?
 
A

Arne Vajhøj

Joza said:
I have question about setting decimal symbols... In some countries,
for example in my country we use comma for decimal symbol, but
in in some other countries it is decimal point. The problem is when
I use comma and want to save data in database.
For example if i want to save number 4,50 it saves like 450, but
if i write 4.50 it regulary saves the number in that format.
My question is how to make to program knows what to act if my app
is working on system where decimal symbol is comma or decimal point.
Or should i use only comma and when saving data i just parse the
number and replace comma with point?

You can either explicit specify CultureInfo for the
Parse or if you have really special requirements
explicit specify a NumberFormatInfo.

Arne
 
J

Jon Skeet [C# MVP]

Joza said:
I have question about setting decimal symbols... In some countries,
for example in my country we use comma for decimal symbol, but
in in some other countries it is decimal point. The problem is when
I use comma and want to save data in database.
For example if i want to save number 4,50 it saves like 450, but
if i write 4.50 it regulary saves the number in that format.
My question is how to make to program knows what to act if my app
is working on system where decimal symbol is comma or decimal point.
Or should i use only comma and when saving data i just parse the
number and replace comma with point?

Don't specify the number as text at all when you pass it to the
database - use a parameterised command, and leave it as a number.
 
M

Michael Nemtsev [MVP]

Hello Joza,

it's called "turkey test" http://www.moserware.com/2008/02/does-your-code-pass-turkey-test.html

just use the .Parse with NumberFormatInfo.Invariant as were recommended

---
WBR,
Michael Nemtsev [.NET/C# MVP] :: blog: http://spaces.live.com/laflour

"The greatest danger for most of us is not that our aim is too high and we
miss it, but that it is too low and we reach it" (c) Michelangelo


J> Hi everybody!
J>
J> I have question about setting decimal symbols... In some countries,
J> for example in my country we use comma for decimal symbol, but
J> in in some other countries it is decimal point. The problem is when
J> I use comma and want to save data in database.
J> For example if i want to save number 4,50 it saves like 450, but
J> if i write 4.50 it regulary saves the number in that format.
J> My question is how to make to program knows what to act if my app
J> is working on system where decimal symbol is comma or decimal point.
J> Or should i use only comma and when saving data i just parse the
J> number and replace comma with point
 
M

Michael Nemtsev [MVP]

J

Jon Skeet [C# MVP]

Michael Nemtsev said:
it's called "turkey test" http://www.moserware.com
/2008/02/does-your-code-pass-turkey-test.html

just use the .Parse with NumberFormatInfo.Invariant as were recommended

Why do that when the aim is to get the number into a *database*? That's
what db command parameters are for :)
 
A

Arne Vajhøj

Jon said:
Why do that when the aim is to get the number into a *database*? That's
what db command parameters are for :)

True.

But before he can do that he need to get it as a number. Which is
where the parse is needed.

Arne
 
J

Jon Skeet [C# MVP]

Arne Vajhøj said:
True.

But before he can do that he need to get it as a number. Which is
where the parse is needed.

There's no indication from the OP's question that he has it as a string
to start with, as far as I can tell.
 
A

Arne Vajhøj

Jon said:
There's no indication from the OP's question that he has it as a string
to start with, as far as I can tell.

The original post was not very specific.

I got associations to the classic:

sqlstr = "INSERT ... VALUES(...," + double.Parse(tb.Text) + ",...)";

But maybe it was more hallucinations than associations ...

:)

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top