Solved

Format currency and percentage

Posted on 2006-11-20
8
8,278 Views
Last Modified: 2011-08-18
hi EE
I would like to format currency and percentages as follows:

1) Currency - thousand seperator and accuracy to 2 decimal places (rounding to 2 decimal points if 3 digits given after the decimal pt)

ex: 3,001.987 --> 3,001.99

2) Percentage - accuracy to 2 decimal places (rounding to 2 decimal points if 3 digits given after the decimal pt)

What would be the correct string format in order to format to these specifications?
thx
0
Comment
Question by:LuckyLucks
  • 4
8 Comments
 
LVL 35

Expert Comment

by:YZlat
ID: 17980011
currency:
String.Format("{0:C}", 1234.56)

percentage

String.Format("{0:P}", .1234)
0
 
LVL 35

Accepted Solution

by:
YZlat earned 500 total points
ID: 17980092
String.Format("{0:C2}", 1234.56)

String.Format("{0:P2}", .1234)
0
 
LVL 7

Expert Comment

by:wtconway
ID: 17981077
What YZlat will work fine. Another example follows. Say, for example, you were storing your values as either Single or Double objects like so:

        Dim myMoney As Double = 2000.356
        Dim myPercent As Double = 0.15797

        myMoney.ToString("c") 'returns $2,000.36
        myPercent.ToString("p") 'returns 15.80 percent as the 9 rounds the 7 to an 8. Unsure how you would force it to remain unrounded

But that's the method I always use since the ToString() function on those datatypes supports it.

Good luck!
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 

Author Comment

by:LuckyLucks
ID: 17981415
YZlat,
   Not sure if I am doing something incorrect but when I try for the percentage an example .07657
it will display it as 7.65 whereas I was looking for 7.66.

Also, since the string format P2 first multiplies the input by 100 and then displays it, something like 7.50 will display as 750.00 , is there a way to turn this multiplication factor off?
0
 
LVL 35

Expert Comment

by:YZlat
ID: 18031800
LuckyLucks, I just tried

Dim number As Double = 0.07657
Label1.Text = String.Format("{0:P2}", number)

and Label1 displayed 7.66%
0
 
LVL 35

Expert Comment

by:YZlat
ID: 18031854
also 7.50 in percentages will be 750.00. That's the way percentage is calculated. What would you want to be displayed instead of 7.5??

The grade you gave me is not fair since I fully answered your question
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Vb.net dynamic formulas in runtime 11 74
Reading the Web.Config using IIS 7.5? 4 35
Creating a route in asp.net webforms 2 25
performance issue in asp web 5 27
Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

815 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now