• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 669
  • Last Modified:

using string.fomat to round to two decimal places

I am splitting a decimal value into dollars and cents

Dollars = Math.Truncate(Total);
Cents = Total - Dollars;

How can I use string.format to get the Cents variable to display just two digits so

DOLLARS = 1343
CENTS = 00

Currently I get  CENTS = 0.0000
0
mugsey
Asked:
mugsey
  • 4
  • 3
  • 3
  • +1
2 Solutions
 
Jorge PaulinoIT Pro/DeveloperCommented:
>> Currently I get  CENTS = 0.0000
And what's the Total ? It looks right your code.
0
 
mugseyAuthor Commented:
I think it maybe that CENTS is a decimal variable?
0
 
Jorge PaulinoIT Pro/DeveloperCommented:
If you have defined the variables as decimal (or double) and the Total is something like 1343.12 then you will have 0.12 in the Cents.
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
mugseyAuthor Commented:
Yes thanks  but I just want the cents to be displayed as  12  and not 0.12
0
 
Göran AnderssonCommented:
You can use formatting to scale a number down, but not up.

Scale the cents value up by two decimals when you calculate it:

Cents = Math.Round((Total - Dollars) * 100);

Then you can use the format string "00" if you want to display it as two digits:

string price = String.Format("{0} dollars and {1:00} cents", Dollars, Cents);
0
 
Jorge PaulinoIT Pro/DeveloperCommented:
>> Yes thanks  but I just want the cents to be displayed as  12  and not 0.12
That's another point and you can do as GreenGhost suggested
0
 
mugseyAuthor Commented:
Thanks but if I use the method above and pass in zero I get the error

Index (zero based) must be greater than or equal to zero and less than the size of the argument list
0
 
Jorge PaulinoIT Pro/DeveloperCommented:
Just check it before do the calculation
0
 
CodeCruiserCommented:
Why not use the following
cents = (0.12).tostring.split(".")(1)
0
 
Göran AnderssonCommented:
> Why not use the following
> cents = (0.12).tostring.split(".")(1)

You would have to specify the culture used for formatting the value into a string. Some cultures format that value into "0,12", which would make the split fail.
0
 
CodeCruiserCommented:
@GreenGhost,
Agreed but mugsey appears to be from Britain so it suggested that method.
0
 
Göran AnderssonCommented:
@CodeCruiser,

Yes, but his server might be hosted in Sweden...

Besides, other people will also take advice from the answers, and they can live anywhere. They may also use it in a windows application, then the culture is dependent on who's running the program, which might come as a nasty surprise...

That's why I wanted to clarify the possible culture issues with the suggestion. :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

  • 4
  • 3
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now