Solved

Decimal with 2 decimal places

Posted on 2013-06-13
5
380 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

828 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