views:

184

answers:

7

How to take away fraction part while formatting decimal type in .NET? I need common syntax for both variants. Is there any gentle solution?

            decimal a = 1.22M;
            decimal b = 1.00M;

            String.Format("${0}", a); // result is $1.22
            String.Format("${0}", b); // result is $1.00, should be $1, HOW?
+2  A: 

Try these - both will output the appropriate currency symbol for the current system:

a.ToString("C2");  // Outputs 2DP
b.ToString("C0"); // Outputs no DP

If you need to supply a specific currency symbol, use the same as above, but substitute N for C.

Paddy
It doesn't answer his question tbh !!!
eugeneK
@eugeneK - in what way - he wanted a common way to output a decimal as a string with specific numbers of decimal places. Which is what this does...
Paddy
Nopes, he wanted to show decimal value with fractions when there are any... "Common syntax for both solutions"
eugeneK
+1 for including currency symbols in the format.
Danny Chen
@eugeneK - you're quite right, coffee just kicking in....
Paddy
@Paddy, then it's unfair because we've got different time zones and i'm already on the second cup...
eugeneK
+1  A: 

The Decimal type is designed to keep track of how many significant digits it has. That is why 1.00M.ToString() returns the string 1.00.

To print a Decimal without the factional part you can use the format specifier N with precision 0:

1.22M.ToString("N0") => "1"
1.00M.ToString("N0") => "1"
1.77M.ToString("N0") => "2"

This will round the Decimal in the conversion process.

Martin Liversage
+3  A: 

In VB.NET I would use

 CINT(INT(a))

I imagine a C# variant exists.

I found a probable solution at this link:

http://www.harding.edu/fmccown/vbnet_csharp_comparison.html

To further explain:

decimal a = 1.55M;
Console.WriteLine("$" & CInt(Int(a)).ToString()); // result is $2

decimal b = 1.22M;
Console.WriteLine("$" & CInt(Int(b)).ToString()); // result is $1

I would steer away from utilizing the currency format as the decimals are inherent to that class.

Michael Eakins
if the value is decimal in first place INT(value) will remove fractions.
eugeneK
CINT(INT(a)) ensures proper rounding
Michael Eakins
Then if the value would be 1.22, it will round to 1 as INT() does when he wants to get 1.22 and just to remove .00 in case where is no fractions
eugeneK
First of all, INT() drops the decimal. There is no rounding.
Jimmie Clark
Second of all, Int() comes out as a Short. The CINT would then convert it to an Integer. CInt in itself actually rounds the number.
Jimmie Clark
This solution is exactly what I would do to drop the decimals off the number.
Jimmie Clark
A: 
string.Format("${0:0}",b)

In C# you can use {0} to tell a parameter, and {0:format} to tell a parameter with format.

EDIT

Oh I thought what OP want to do is removing the digits of b. But now I realized that he wants to remove useless zeroes.

string.Format("${0:#.##}",b)
Danny Chen
+6  A: 

Assuming that 'common syntax' means that you need one solution to give both outputs, String.Format("${0:#.##}", x) does the trick. When x is 1.00M, the result will be "$1". when x is 1.22M, the result is "$1.22".

Joren
But if that thing is 0, the output is empty. I got "$" instead of "$0".
@user337085, try my approach which is not elegant as formatting but will work in any case... String.Format("${0}", anyvalue.Replace(".00",string.Empty));
eugeneK
@user337085: Use `0.##` instead of `#.##`. That will fix your problem when the value is zero.
Martin Liversage
wow, that's cool, it works! thanks Martin and others!
A: 

There are other issues here I think. If the question is to completely ignore decimal places, then just casting to an integer would produce the required output, but would obviously loose precision, which is not a good thing.

There are also rounding considerations when formatting as a string like example below.

decimal a = 1.55M;
Console.WriteLine(a.ToString("C0")); // result is $2

decimal b = 1.22M;
Console.WriteLine( b.ToString( "C0" ) ); // result is $1
Matt
A: 

I presume that this is just for dispaly and not for changing data type to INT when number has no value after decimal.

using System;

namespace stackOverflow
{
    class Program
    {
        static void Main(string[] args)
        {
            decimal a = 1.2245M;
            decimal b = 1.00M;



            Console.WriteLine("Your percentage to date is: {0:#.#####}", a);
            Console.WriteLine("Your percentage to date is: {0:#.#####}", b);//#.#### gives number upto 4 decimal
            Console.ReadLine();

        }


    } 

}
Ashwani Roy