We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,031 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Decimal places and string formatting of Double Values

Greetings.

Ok so my problem is that I get a "Currency" formatted value from the database and this value sometimes comes as a whole number (without decimals) and sometimes it doesn't.

But, when I output the information as "string" into an XML File, I always need to show the 2 decimal places even if they are zero.

I tried with the following code:

price = string.Format("{0:N2", decimalPrice);

This works if e.g: the variable decimalPrice has a value of 59.33
However if it has a value of 59 this will not work, and will throw a format exception.

Any suggestions?

Thanks,

CB

1
Contributor
1
Reply
33 Minutes
Discussion Span
1 Year Ago
Last Updated
2
Views
Question
Answered
charlybones
Junior Poster in Training
77 posts since Jan 2011
Reputation Points: 16
Solved Threads: 16
Skill Endorsements: 0

Solved it with

price = string.Format("{0:0.00}", decimalPrice);
charlybones
Junior Poster in Training
77 posts since Jan 2011
Reputation Points: 16
Solved Threads: 16
Skill Endorsements: 0
Question Self-Answered as of 1 Year Ago

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0570 seconds using 2.69MB