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
5
Medium Priority
?
389 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 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

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Article by: Ivo
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…

704 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