简体   繁体   English

如何从小数中取出分数部分?

[英]How to take away fraction part from decimal?

How to take away fraction part while formatting decimal type in .NET? 如何在.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. 假设'common syntax'意味着你需要一个解决方案来提供两个输出, String.Format("${0:#.##}", x)就可以了。 When x is 1.00M , the result will be "$1" . x1.00M ,结果为"$1" when x is 1.22M , the result is "$1.22" . x1.22M ,结果为"$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. 如果您需要提供特定的货币符号,请使用与上面相同的内容,但将N替换为C.

The Decimal type is designed to keep track of how many significant digits it has. Decimal类型旨在跟踪它有多少有效数字。 That is why 1.00M.ToString() returns the string 1.00 . 这就是1.00M.ToString()返回字符串1.00

To print a Decimal without the factional part you can use the format specifier N with precision 0 : 要打印没有派系部分的Decimal ,可以使用格式说明符N ,精度为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. 这将在转换过程中舍入Decimal

In VB.NET I would use 在VB.NET中我会用

 CINT(INT(a))

I imagine a C# variant exists. 我想象存在一个C#变种。

I found a probable solution at this link: 我在这个链接上找到了一个可能的解决方案:

http://www.harding.edu/fmccown/vbnet_csharp_comparison.html 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. 在C#中,您可以使用{0}来告诉参数,使用{0:format}来告诉参数格式。

EDIT 编辑

Oh I thought what OP want to do is removing the digits of b. 哦,我认为OP想要做的是删除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. 我认为这只是为了显示而不是在数字在十进制之后没有值时将数据类型更改为INT。

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

        }


    } 

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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