98

我想以整数的形式接收小数点后的数字。例如,只有 05 来自 1.05 或来自 2.50 只有 50而不是0.50

4

19 回答 19

223

最好的方法是:

var floatNumber = 12.5523;

var x = floatNumber - Math.Truncate(floatNumber);

结果你可以随心所欲地转换

于 2014-09-03T10:23:59.107 回答
87
var decPlaces = (int)(((decimal)number % 1) * 100);

这假定您的数字只有两位小数。

于 2012-10-23T20:16:21.620 回答
43

有一个比“Math.Truncate”方法更干净、更快捷的解决方案:

double frac = value % 1;
于 2018-07-03T23:27:38.063 回答
18

无舍入问题的解决方案:

double number = 10.20;
var first2DecimalPlaces = (int)(((decimal)number % 1) * 100);
Console.Write("{0:00}", first2DecimalPlaces);

输出: 20

请注意,如果我们没有转换为十进制,它将输出19.

还:

  • 对于318.40输出:(40而不是39
  • 对于47.612345输出:(61而不是612345
  • 对于3.01输出:(01而不是1

如果您正在处理财务数字,例如,如果在这种情况下您试图获取交易金额的美分部分,请始终使用decimal数据类型。

更新:

如果将其作为字符串处理(基于@SearchForKnowledge 的答案),则以下内容也将起作用。

10.2d.ToString("0.00", CultureInfo.InvariantCulture).Split('.')[1]

然后,您可以使用Int32.Parse将其转换为 int。

于 2015-07-13T18:47:34.767 回答
10

更好的方法 -

        double value = 10.567;
        int result = (int)((value - (int)value) * 100);
        Console.WriteLine(result);

输出 -

56
于 2012-10-23T20:22:28.390 回答
9

最简单的变体可能是 Math.truncate()

double value = 1.761
double decPart = value - Math.truncate(value)
于 2015-08-05T12:01:19.523 回答
3

我猜这个帖子已经老了,但我不敢相信没有人提到过 Math.Floor

//will always be .02 cents
(10.02m - System.Math.Floor(10.02m))
于 2019-12-19T21:17:51.227 回答
1
var result = number.ToString().Split(System.Globalization.NumberDecimalSeparator)[2]

将其作为字符串返回(但您始终可以将其转换回 int),并假定该数字确实有一个“。” 某处。

于 2012-10-23T20:18:01.373 回答
1
int last2digits = num - (int) ((double) (num /  100) * 100);
于 2015-04-02T04:59:19.253 回答
0
    public static string FractionPart(this double instance)
    {
        var result = string.Empty;
        var ic = CultureInfo.InvariantCulture;
        var splits = instance.ToString(ic).Split(new[] { ic.NumberFormat.NumberDecimalSeparator }, StringSplitOptions.RemoveEmptyEntries);
        if (splits.Count() > 1)
        {
            result = splits[1];
        }
        return result;
    }
于 2013-11-10T20:12:46.320 回答
0
 string input = "0.55";
    var regex1 = new System.Text.RegularExpressions.Regex("(?<=[\\.])[0-9]+");
    if (regex1.IsMatch(input))
    {
        string dp= regex1.Match(input ).Value;
    }
于 2020-11-09T12:38:59.573 回答
0
var d = 1.5;
var decimalPart = Convert.ToInt32(d.ToString().Split('.')[1]);

它给你5来自1.5:)

于 2021-10-25T20:48:21.970 回答
-1

.您可以在将双精度转换为字符串后从您尝试使用函数获取小数的双精度中删除点,Remove()以便您可以对其进行所需的操作

考虑有一个双倍_Double的值0.66781,下面的代码将只显示点后面的数字,.它们是66781

double _Double = 0.66781; //Declare a new double with a value of 0.66781
string _Decimals = _Double.ToString().Remove(0, _Double.ToString().IndexOf(".") + 1); //Remove everything starting with index 0 and ending at the index of ([the dot .] + 1) 

另一种解决方案

您也可以使用Path以跨平台方式对字符串实例执行操作的类

double _Double = 0.66781; //Declare a new double with a value of 0.66781
string Output = Path.GetExtension(D.ToString()).Replace(".",""); //Get (the dot and the content after the last dot available and replace the dot with nothing) as a new string object Output
//Do something
于 2012-10-23T20:22:41.100 回答
-1

这是我为类似情况编写的扩展方法。我的应用程序将收到 2.3 或 3.11 格式的数字,其中数字的整数部分表示年,小数部分表示月。

// Sample Usage
int years, months;
double test1 = 2.11;
test1.Split(out years, out months);
// years = 2 and months = 11

public static class DoubleExtensions
{
    public static void Split(this double number, out int years, out int months)
    {
        years = Convert.ToInt32(Math.Truncate(number));

        double tempMonths = Math.Round(number - years, 2);
        while ((tempMonths - Math.Floor(tempMonths)) > 0 && tempMonths != 0) tempMonths *= 10;
        months = Convert.ToInt32(tempMonths);
    }
}
于 2017-04-05T16:02:21.213 回答
-1

在我的测试中,这比 Math.Truncate 答案慢 3-4 倍,但只有一个函数调用。也许有人喜欢它:

var float_number = 12.345;
var x = Math.IEEERemainder(float_number , 1)
于 2021-04-26T14:56:46.583 回答
-2

使用正则表达式:Regex.Match("\.(?\d+)")如果我在这里错了,请有人纠正我

于 2012-10-23T20:22:13.727 回答
-2

这很简单

       float moveWater =  Mathf.PingPong(theTime * speed, 100) * .015f;
       int    m = (int)(moveWater);
       float decimalPart= moveWater -m ;

       Debug.Log(decimalPart);
于 2015-04-18T14:16:57.810 回答
-2

为什么不使用int y = value.Split('.')[1];

Split()函数将值拆分为单独的内容,然后1输出第二个值.

于 2015-07-13T19:33:16.910 回答
-19

更新的答案

在这里,我给出了 3 种相同的方法。

[1] 使用Math.Truncate 的数学解法

 var float_number = 12.345;
 var result = float_number - Math.Truncate(float_number);

//输入:1.05
//输出:“0.050000000000000044”

// 输入:10.2
// 输出:0.19999999999999929

如果这不是您期望的结果,那么您必须将结果更改为您想要的形式(但您可能会再次进行一些字符串操作。)

[2] 使用乘数 [这是 10 的 N 次方(例如 10² 或 10³),其中 N 是小数位数]

       // multiplier is " 10 to the power of 'N'" where 'N' is the number 
       // of decimal places
       int multiplier = 1000;  
       double double_value = 12.345;
       int double_result = (int)((double_value - (int)double_value) * multiplier);

// 输出 345

如果小数位数不固定,那么这种方法可能会产生问题。

[3] 使用“正则表达式(REGEX)”

在使用字符串编写解决方案时,我们应该非常小心。除某些情况外,这不是可取的。

如果您要使用小数位进行一些字符串操作,那么这将是可取的

    string input_decimal_number = "1.50";
    var regex = new System.Text.RegularExpressions.Regex("(?<=[\\.])[0-9]+");
    if (regex.IsMatch(input_decimal_number))
    {
        string decimal_places = regex.Match(input_decimal_number).Value;
    }

// 输入:“1.05”
// 输出:“05”

// 输入:“2.50”
// 输出:“50”

// 输入:“0.0550”
// 输出:“0550”

您可以在http://www.regexr.com/上找到有关 Regex 的更多信息

于 2013-10-15T06:14:54.247 回答