?
Solved

using string.fomat to round to two decimal places

Posted on 2009-07-15
12
Medium Priority
?
665 Views
Last Modified: 2012-05-07
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
Comment
Question by:mugsey
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 3
  • +1
12 Comments
 
LVL 48

Expert Comment

by:jpaulino
ID: 24858581
>> Currently I get  CENTS = 0.0000
And what's the Total ? It looks right your code.
0
 

Author Comment

by:mugsey
ID: 24858602
I think it maybe that CENTS is a decimal variable?
0
 
LVL 48

Assisted Solution

by:jpaulino
jpaulino earned 400 total points
ID: 24858621
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:mugsey
ID: 24858628
Yes thanks  but I just want the cents to be displayed as  12  and not 0.12
0
 
LVL 29

Accepted Solution

by:
Göran Andersson earned 1600 total points
ID: 24858633
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
 
LVL 48

Expert Comment

by:jpaulino
ID: 24858666
>> 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
 

Author Comment

by:mugsey
ID: 24858687
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
 
LVL 48

Expert Comment

by:jpaulino
ID: 24858710
Just check it before do the calculation
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 24859278
Why not use the following
cents = (0.12).tostring.split(".")(1)
0
 
LVL 29

Expert Comment

by:Göran Andersson
ID: 24861389
> 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
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 24867279
@GreenGhost,
Agreed but mugsey appears to be from Britain so it suggested that method.
0
 
LVL 29

Expert Comment

by:Göran Andersson
ID: 24868210
@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

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question