简体   繁体   中英

How to take away fraction part from decimal?

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?

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" .

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.

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.

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.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {

        static void Main(string[] args)
        {
            double n, x;
            int a, dec = 0;
            Console.WriteLine("Enter double");
            n = Convert.ToDouble(Console.ReadLine());

            a = Convert.ToInt32(n);
            x = n - a;
            if (x < 0)
                a--;
            int k = 1000;
            for (int i = 0; i < n.ToString().Length; i++)
            {
                if (n.ToString()[i] == '.')
                    k = i;
                if (i > k)
                    dec = dec * 10 + (n.ToString()[i]-48);
            }

            Console.WriteLine("Non-fraction " + a);
            Console.WriteLine("Fraction " + dec);


            Console.ReadKey();

        }


    }
}
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)

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

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();

        }


    } 

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM