Solved

Decimal with 2 decimal places

Posted on 2013-06-13
5
383 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
[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
  • 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

Independent Software Vendors: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
linq .concat equivalent with expression syntax 4 42
Set custom font WPF 2 25
insert an Jpeg/bmp image onto the visio drawing using VBA/C# 5 67
c#, datatable, aspx 4 48
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
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.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

737 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