Solved

Decimal with 2 decimal places

Posted on 2013-06-13
5
368 Views
Last Modified: 2013-06-14
I need to get a Decimal into a format with 2 decimal places and without a currency sign (like $). I have this (where amount is Decimal):
string myString = (String.Format ("{0:.##}", this.amount));
decimal decimalRounded = Decimal.Parse(this.amount.ToString("0.00"));
return decimalRounded;

Open in new window

but I still get numbers like 1234.5, not 1234.50.
How can this be done?
0
Comment
Question by:allelopath
  • 3
5 Comments
 
LVL 21

Accepted Solution

by:
Craig Wagner earned 500 total points
ID: 39246190
You can't really format a "Decimal", you can only format it when you turn it into a string. When it is in its numeric form then 1234.5 is accurate. If you want to display that number as 1234.50 then you need to do what you're already doing, which is String.Format it.
0
 
LVL 9

Expert Comment

by:sarabhai
ID: 39246605
decimal d = 1234.5678;
d = decimal.Round(d, 2);
Console.WriteLine("decimal variable d value: {0}", d); // Prints 1234.57
0
 
LVL 1

Author Comment

by:allelopath
ID: 39247509
@sarabhai:
The first line in the code snippet doesn't compile:
Cannot convert source type 'double' to target type 'decimal'
0
 
LVL 1

Author Comment

by:allelopath
ID: 39247548
@CraigWagner:
I see what you are saying. So I created a getter to return a string:
public string AmountString
{
    get
        {
            string myString = (String.Format("{0:.##}", this.amount));
            return myString;
    }
}
If the value has no decimal places, e.g. 1234, it returns 1234, not 1234.00
If it has 1 decimal place, e.g. 1234.1, it returns 1234.1, not 1234.10
0
 
LVL 1

Author Comment

by:allelopath
ID: 39248818
This works:
string myString = (String.Format("{0:0.00}", this.amount));
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

758 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

23 Experts available now in Live!

Get 1:1 Help Now