![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Dec 2005
Posts: 40
Rep Power: 0
![]() |
String to decimal.
I want to add information to a database using a form. I want to take 3 bits of information - Name, Price, Quantity.
Name must be stored in the DB as text, Price as currency and quantity as Number. For name ive got: string name = TextBox1.Text; For Quantity ive got: int quantity = Int32.Parse(TextBox3.Text); But i don't know what to do for currency. I had something like this: System.Decimal price = TextBox2.Text; It gives this error: Cannot implicitly convert type 'string' to 'decimal'. I know what the problem is, i just don't know the correct code. |
|
|
|
|
|
#2 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 893
Rep Power: 4
![]() |
Yes, parse it like the int one, except use the Decimal.Parse method.
|
|
|
|
|
|
#3 |
|
Programmer
Join Date: Jun 2005
Posts: 99
Rep Power: 4
![]() |
A decimal is a type just like Int32/float etc... therefore it will have a Parse() method and/or ToString(). On side note why are you using a decimal (an 80bit floating point number) to store price? it seems like overkill.
|
|
|
|
|
|
#4 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
When parsing data entered by the user, it would probably be better to use the TryParse method. Instead of throwing an exception on failure, this method simply returns false.
|
|
|
|
|
|
#5 | |
|
Hobbyist Programmer
Join Date: Oct 2005
Posts: 134
Rep Power: 4
![]() |
Quote:
2. Quote from MSDN: "The Decimal value type is appropriate for financial calculations requiring large numbers of significant integral and fractional digits and no round-off errors." In other words, it uses what's called "decimal arithmetic" so there is no loss of precision like you get with float and doubles. |
|
|
|
|
|
|
#6 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Lord knows one wouldn't want rounding errors in a dollar-two-ninety-eight! :eek:
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#7 | |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
Quote:
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for." -- Socrates |
|
|
|
|
|
|
#8 |
|
Newbie
|
I'd recommend using float... It's proven reliable for me till now
float price = float.Parse(this.Textbox2.Text); Then I don't know what DB you're using, coz I've had problems with SQL2000 datatypes before. |
|
|
|
|
|
#9 |
|
Programmer
Join Date: Dec 2005
Posts: 40
Rep Power: 0
![]() |
Thanks for that codelord i'll give that a go. Is the - this. - part neccessary though?
im just using access for the DB. |
|
|
|
|
|
#10 |
|
Newbie
Join Date: Sep 2005
Posts: 14
Rep Power: 0
![]() |
__________________
http://www.codeblank.net/ - Coding Forums with Syntax Highlighter and Random Post count http://www.jorgepena.be/ - Personal Site |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|