How to retain a decimal?

G

Guest

Hello.

If a convert a string (string number = "24.45") into a double, using the
Convert class ((double doublenumber = Convert.ToDouble(number)), the result
will have the value 2445.

How do I retain the correct value?
 
A

Alvin Bruney [MVP - ASP.NET]

..net offers the decimal type. use it instead of a double

--
Regards,
Alvin Bruney
[Shameless Author Plug]
The Microsoft Office Web Components Black Book with .NET
available at www.lulu.com/owc, Amazon, B&H etc


Forth-coming VSTO.NET
 
G

Guest

That doesn't help one bit.

decimal decimalnumber = Convert.ToDecimal("25.34");

decimalnumber will have the value 2534 after this. I have tried it already.


This problem that I receive should be quite common.

As example, you have a textbox on a webform, and you need to enter a price
for a product. If you convert the text of the textbox, the problem should
clear:

User entered the value 25.55 into the textbox.
double number = Convert.ToDouble(textbox.text);

number has the value 2555. How to retain the value 25.55 ?
 
C

Chris Botha

This problem that I receive should be quite common.

No, it is not that common, 1st time I've seen this.
put a breakpoint in your code and examine the values, I think you are
passing in "2534".


"the friendly display name"
That doesn't help one bit.

decimal decimalnumber = Convert.ToDecimal("25.34");

decimalnumber will have the value 2534 after this. I have tried it
already.


This problem that I receive should be quite common.

As example, you have a textbox on a webform, and you need to enter a price
for a product. If you convert the text of the textbox, the problem should
clear:

User entered the value 25.55 into the textbox.
double number = Convert.ToDouble(textbox.text);

number has the value 2555. How to retain the value 25.55 ?


Alvin Bruney said:
..net offers the decimal type. use it instead of a double

--
Regards,
Alvin Bruney
[Shameless Author Plug]
The Microsoft Office Web Components Black Book with .NET
available at www.lulu.com/owc, Amazon, B&H etc


Forth-coming VSTO.NET
 
A

Alvin Bruney [MVP - ASP.NET]

decimal decimalnumber = Convert.ToDecimal("25.34");
provides a value of "25.34".
decimalnumber will have the value 2534 after this. I have tried it
already.
Nope, you could not have possibly tried it!

--
Regards,
Alvin Bruney
[Shameless Author Plug]
The Microsoft Office Web Components Black Book with .NET
available at www.lulu.com/owc, Amazon, B&H etc


Forth-coming VSTO.NET
-------------------------------------------------------------------------------
"the friendly display name"
That doesn't help one bit.

decimal decimalnumber = Convert.ToDecimal("25.34");

decimalnumber will have the value 2534 after this. I have tried it
already.


This problem that I receive should be quite common.

As example, you have a textbox on a webform, and you need to enter a price
for a product. If you convert the text of the textbox, the problem should
clear:

User entered the value 25.55 into the textbox.
double number = Convert.ToDouble(textbox.text);

number has the value 2555. How to retain the value 25.55 ?


Alvin Bruney said:
..net offers the decimal type. use it instead of a double

--
Regards,
Alvin Bruney
[Shameless Author Plug]
The Microsoft Office Web Components Black Book with .NET
available at www.lulu.com/owc, Amazon, B&H etc


Forth-coming VSTO.NET
 
J

Juan T. Llibre

That's very odd.

When I try it, decimalnumber returns 25.34






the friendly display name said:
That doesn't help one bit.

decimal decimalnumber = Convert.ToDecimal("25.34");

decimalnumber will have the value 2534 after this. I have tried it already.


This problem that I receive should be quite common.

As example, you have a textbox on a webform, and you need to enter a price
for a product. If you convert the text of the textbox, the problem should
clear:

User entered the value 25.55 into the textbox.
double number = Convert.ToDouble(textbox.text);

number has the value 2555. How to retain the value 25.55 ?


Alvin Bruney said:
..net offers the decimal type. use it instead of a double

--
Regards,
Alvin Bruney
[Shameless Author Plug]
The Microsoft Office Web Components Black Book with .NET
available at www.lulu.com/owc, Amazon, B&H etc


Forth-coming VSTO.NET
 
D

Damien

the said:
That doesn't help one bit.

decimal decimalnumber = Convert.ToDecimal("25.34");

decimalnumber will have the value 2534 after this. I have tried it already.


This problem that I receive should be quite common.

As example, you have a textbox on a webform, and you need to enter a price
for a product. If you convert the text of the textbox, the problem should
clear:

User entered the value 25.55 into the textbox.
double number = Convert.ToDouble(textbox.text);

number has the value 2555. How to retain the value 25.55 ?
Sounds like some kind of locale/I18N problem. What happens if you try
to convert "25,55" to decimal?

Damien
 

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
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top