Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 10th, 2006, 5:53 AM   #1
BungalowBill
Programmer
 
Join Date: Dec 2005
Posts: 40
Rep Power: 0 BungalowBill is on a distinguished road
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;
which works.

For Quantity ive got:

int quantity         = Int32.Parse(TextBox3.Text);
which i think works.

But i don't know what to do for currency.

I had something like this:
System.Decimal price = TextBox2.Text;
but it doesn't like it. Do i have to parse it like the int quantity one?

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.
BungalowBill is offline   Reply With Quote
Old Apr 10th, 2006, 6:47 AM   #2
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 893
Rep Power: 4 The Dark is on a distinguished road
Yes, parse it like the int one, except use the Decimal.Parse method.
The Dark is offline   Reply With Quote
Old Apr 10th, 2006, 6:52 AM   #3
Animatronic
Programmer
 
Join Date: Jun 2005
Posts: 99
Rep Power: 4 Animatronic is on a distinguished road
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.
Animatronic is offline   Reply With Quote
Old Apr 10th, 2006, 6:53 AM   #4
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
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.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Apr 10th, 2006, 12:30 PM   #5
Kaja Fumei
Hobbyist Programmer
 
Join Date: Oct 2005
Posts: 134
Rep Power: 4 Kaja Fumei is on a distinguished road
Quote:
Originally Posted by Animatronic
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.
1. decimal is 128 bits.
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.
Kaja Fumei is offline   Reply With Quote
Old Apr 10th, 2006, 12:47 PM   #6
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Apr 10th, 2006, 4:01 PM   #7
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
Quote:
Originally Posted by DaWei
Lord knows one wouldn't want rounding errors in a dollar-two-ninety-eight! :eek:
Yeah, I don't think a banking application is going to be written in C# anyway. I might be wrong though.
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Old Apr 10th, 2006, 4:51 PM   #8
TheCodeLord
Newbie
 
Join Date: Apr 2006
Posts: 7
Rep Power: 0 TheCodeLord is on a distinguished road
Send a message via ICQ to TheCodeLord Send a message via MSN to TheCodeLord Send a message via Yahoo to TheCodeLord
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.
TheCodeLord is offline   Reply With Quote
Old Apr 12th, 2006, 5:36 AM   #9
BungalowBill
Programmer
 
Join Date: Dec 2005
Posts: 40
Rep Power: 0 BungalowBill is on a distinguished road
Thanks for that codelord i'll give that a go. Is the - this. - part neccessary though?

im just using access for the DB.
BungalowBill is offline   Reply With Quote
Old Apr 13th, 2006, 3:09 AM   #10
blank
Newbie
 
Join Date: Sep 2005
Posts: 14
Rep Power: 0 blank is on a distinguished road
I always just do

decimal thing = Convert.ToDecimal(mystring);
__________________
http://www.codeblank.net/ - Coding Forums with Syntax Highlighter and Random Post count
http://www.jorgepena.be/ - Personal Site
blank is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:10 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC