Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# Decimal with 2 decimal places

Posted on 2013-06-13
Medium Priority
389 Views
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;
``````
but I still get numbers like 1234.5, not 1234.50.
How can this be done?
0
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
• 3

LVL 21

Accepted Solution

Craig Wagner earned 2000 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

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

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

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

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

## Featured Post

Question has a verified solution.

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

C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
###### Suggested Courses
Course of the Month7 days, 6 hours left to enroll