read sql server's decimal value to asp.net double? How?

G

Guest

Hi everyone!

I'm using greece - greek in my control panel's regional options,
and so, my decimal point is the comma (,),
while it is the dot (.) for the sql server db,

however, I'm facing trouble when I need to parse the sql server decimal as
an asp.net double!

For example:
Code:
?Double.Parse("0.03", New System.Globalization.CultureInfo("el-gr"))
3.0
?Double.Parse("0.03", New System.Globalization.CultureInfo("en-US"))
0.03

?Double.Parse("1,0", New System.Globalization.CultureInfo("el-gr"))
1.0
?Double.Parse("1,0", New System.Globalization.CultureInfo("en-US"))
10.0

The first culture has problems with the dot (.),
while the second has problems with the comma (,)!

How on earth am I supposed to do this thing?

Thanks in advance!
 
H

Hans Kesting

Hi everyone!
I'm using greece - greek in my control panel's regional options,
and so, my decimal point is the comma (,),
while it is the dot (.) for the sql server db,

however, I'm facing trouble when I need to parse the sql server decimal as
an asp.net double!

For example:
Code:
?Double.Parse("0.03", New System.Globalization.CultureInfo("el-gr"))
3.0
?Double.Parse("0.03", New System.Globalization.CultureInfo("en-US"))
0.03

?Double.Parse("1,0", New System.Globalization.CultureInfo("el-gr"))
1.0
?Double.Parse("1,0", New System.Globalization.CultureInfo("en-US"))
10.0

The first culture has problems with the dot (.),
while the second has problems with the comma (,)!

How on earth am I supposed to do this thing?

Thanks in advance!

If the value in SqlServer really is a decimal, I think it is better to
stick to numbers and not mess with strings inbetween.

So: the "object" value that is returned in the dataset needs to be cast
to a decimal, which can then be cast to a double. So something like:

double mydouble =
(double)(decimal)MyDataTable.Rows[0]["mydecimalcolumn"];

Hans Kesting
 
G

Guest

same thing with your suggestion,
works with "1,0" but doesn't work with "0.03" (the last is converted to 3.0)!

Any other suggestions?

Hans Kesting said:
Hi everyone!

I'm using greece - greek in my control panel's regional options,
and so, my decimal point is the comma (,),
while it is the dot (.) for the sql server db,

however, I'm facing trouble when I need to parse the sql server decimal as
an asp.net double!

For example:
Code:
?Double.Parse("0.03", New System.Globalization.CultureInfo("el-gr"))
3.0
?Double.Parse("0.03", New System.Globalization.CultureInfo("en-US"))
0.03

?Double.Parse("1,0", New System.Globalization.CultureInfo("el-gr"))
1.0
?Double.Parse("1,0", New System.Globalization.CultureInfo("en-US"))
10.0

The first culture has problems with the dot (.),
while the second has problems with the comma (,)!

How on earth am I supposed to do this thing?

Thanks in advance!

If the value in SqlServer really is a decimal, I think it is better to
stick to numbers and not mess with strings inbetween.

So: the "object" value that is returned in the dataset needs to be cast
to a decimal, which can then be cast to a double. So something like:

double mydouble =
(double)(decimal)MyDataTable.Rows[0]["mydecimalcolumn"];

Hans Kesting
 
H

Hans Kesting

same thing with your suggestion,
works with "1,0" but doesn't work with "0.03" (the last is converted to 3.0)!

Any other suggestions?

Some questions:
- what is the columntype in sqlserver? ((n)varchar or decimal?)
- how are you retrieving the value from the table into your C# code?
(just a straight 'select <columnname>' from the table, or maybe a
'convert(varchar, <column>)' somewhere?)
- does anything happen with that value between retrieving and
converting? (a "ToString()" somewhere?)
- how are you trying to convert it to that final double?

*if* the value is really stored as decimal inside sqlserver, then it's
not stored as "0.03" (that is, with a "decimal dot" as opposed to a
"decimal comma"). It is stored as some binary code. Only when you
*display* the value, the software converting the binary value to a
string will decide between comma and dot.
So if you are not converting it to a string somewhere inbetween, you
should have no problems with culture specific decimal markers.

Hans Kesting

Hans Kesting said:
Hi everyone!

I'm using greece - greek in my control panel's regional options,
and so, my decimal point is the comma (,),
while it is the dot (.) for the sql server db,

however, I'm facing trouble when I need to parse the sql server decimal as
an asp.net double!

For example:
Code:
?Double.Parse("0.03", New System.Globalization.CultureInfo("el-gr"))
3.0
?Double.Parse("0.03", New System.Globalization.CultureInfo("en-US"))
0.03

?Double.Parse("1,0", New System.Globalization.CultureInfo("el-gr"))
1.0
?Double.Parse("1,0", New System.Globalization.CultureInfo("en-US"))
10.0

The first culture has problems with the dot (.),
while the second has problems with the comma (,)!

How on earth am I supposed to do this thing?

Thanks in advance!

If the value in SqlServer really is a decimal, I think it is better to
stick to numbers and not mess with strings inbetween.

So: the "object" value that is returned in the dataset needs to be cast
to a decimal, which can then be cast to a double. So something like:

double mydouble =
(double)(decimal)MyDataTable.Rows[0]["mydecimalcolumn"];

Hans Kesting
 
G

Guest

yes you are right!

that was it, I found it yesterday,
my pc has "," as decimal point,
while I stored the other value in the db (as varchar) using "." as decimal,
confused with how sql server stores decimals!

Anyway, thanks all for your answers guys!

Hans Kesting said:
same thing with your suggestion,
works with "1,0" but doesn't work with "0.03" (the last is converted to 3.0)!

Any other suggestions?

Some questions:
- what is the columntype in sqlserver? ((n)varchar or decimal?)
- how are you retrieving the value from the table into your C# code?
(just a straight 'select <columnname>' from the table, or maybe a
'convert(varchar, <column>)' somewhere?)
- does anything happen with that value between retrieving and
converting? (a "ToString()" somewhere?)
- how are you trying to convert it to that final double?

*if* the value is really stored as decimal inside sqlserver, then it's
not stored as "0.03" (that is, with a "decimal dot" as opposed to a
"decimal comma"). It is stored as some binary code. Only when you
*display* the value, the software converting the binary value to a
string will decide between comma and dot.
So if you are not converting it to a string somewhere inbetween, you
should have no problems with culture specific decimal markers.

Hans Kesting

Hans Kesting said:
Hi everyone!

I'm using greece - greek in my control panel's regional options,
and so, my decimal point is the comma (,),
while it is the dot (.) for the sql server db,

however, I'm facing trouble when I need to parse the sql server decimal as
an asp.net double!

For example:
Code:
?Double.Parse("0.03", New System.Globalization.CultureInfo("el-gr"))
3.0
?Double.Parse("0.03", New System.Globalization.CultureInfo("en-US"))
0.03

?Double.Parse("1,0", New System.Globalization.CultureInfo("el-gr"))
1.0
?Double.Parse("1,0", New System.Globalization.CultureInfo("en-US"))
10.0

The first culture has problems with the dot (.),
while the second has problems with the comma (,)!

How on earth am I supposed to do this thing?

Thanks in advance!

If the value in SqlServer really is a decimal, I think it is better to
stick to numbers and not mess with strings inbetween.

So: the "object" value that is returned in the dataset needs to be cast
to a decimal, which can then be cast to a double. So something like:

double mydouble =
(double)(decimal)MyDataTable.Rows[0]["mydecimalcolumn"];

Hans Kesting
 

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

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top