Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从double中获取小数部分_C#_.net - Fatal编程技术网

C# 从double中获取小数部分

C# 从double中获取小数部分,c#,.net,C#,.net,我想要以整数的形式接收小数点后的数字。例如,从1.05开始只有05或从2.50开始只有50not0.50 var decPlaces = (int)(((decimal)number % 1) * 100); 这假定您的数字只有两位小数 var result = number.ToString().Split(System.Globalization.NumberDecimalSeparator)[2] 将其作为字符串返回(但您始终可以将其转换为int),并假设数字的某个位置有“.”。使用正

我想要以整数的形式接收小数点后的数字。例如,从1.05开始只有05或从2.50开始只有50not0.50

var decPlaces = (int)(((decimal)number % 1) * 100);
这假定您的数字只有两位小数

var result = number.ToString().Split(System.Globalization.NumberDecimalSeparator)[2]

将其作为字符串返回(但您始终可以将其转换为int),并假设数字的某个位置有“.”。

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

更好的方法-

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

56

在将double转换为字符串后,您可以从double中删除double
,以尝试使用
remove()
函数获取小数,从而可以对其执行所需的操作

考虑使用值为
0.66781
的double
\u double
,以下代码将仅显示点
后的数字,即
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

更新的答案

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

[1]使用

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

//输入:10.2
//输出:0.19999999929

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

[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)”

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

如果要执行一些带小数位的字符串操作,则最好使用

字符串输入\u decimal\u number=“1.50”;

var regex=new System.Text.RegularExpressions.regex(“(?最好的方式是:

var floatNumber = 12.5523;

var x = floatNumber - Math.Truncate(floatNumber);
结果您可以随意转换它非常简单

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

       Debug.Log(decimalPart);

无舍入问题的解决方案:

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
数据类型

更新:

double number = 10.20;
var first2DecimalPlaces = (int)(((decimal)number % 1) * 100);
Console.Write("{0:00}", first2DecimalPlaces);
如果将其作为字符串处理(基于@SearchForKnowledge的答案),则以下内容也可以使用


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

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


Split()


这是我为类似情况编写的扩展方法。我的应用程序将接收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);
    }
}

与“Math.Truncate”方法相比,有一种更干净、更快的解决方案:

double frac = value % 1;

这可能是开销,但应该有效

double yourDouble = 1.05;
string stringForm = yourDouble.ToString();
int dotPosition = stringForm.IndexOf(".");
decimal decimalPart = Decimal.Parse("0" + stringForm.Substring(dotPosition));

Console.WriteLine(decimalPart); // 0.05

我猜这条线已经过时了,但我不敢相信没有人提到数学

//will always be .02 cents
(10.02m - System.Math.Floor(10.02m))
string input=“0.55”;

var regex1=new System.Text.RegularExpressions.Regex(“(?)在我的测试中,这比数学慢3-4倍。截断答案,但只有一个函数调用。也许有人喜欢它:

var float_number = 12.345;
var x = Math.IEEERemainder(float_number , 1)

2.50只是2.5。我想你说的是字符串。你总是返回小数点后的前两位吗?输入是不是一个
十进制数
浮点数
字符串
,…?一个用法的例子对下一次得到高质量的答案有很大帮助。是的,只有零位,输入是十进制的团队应该感到尴尬(并采取行动),因为这个非常常见的操作没有单一的Math.function,比如Math.DecimalPart(双x)或其他什么。有太多的“你试过这个了吗,你想过那个了吗?”对于我们许多人都需要经常做的事情。在
Regex.Match
中不太流行,但我在尝试获取十进制
ArgumentException的值时收到以下错误:解析“\。(?\d+”-无法识别的分组结构。
此解决方案比本页上的任何其他解决方案大约慢两个数量级,比我自己的答案慢四个数量级。此解决方案是特定于文化的。例如,在俄语和英语美国,有不同的小数分隔符(相应的逗号和点)。因此,此代码将在其他区域性设置上失败。此解决方案在
上拆分,而这并不总是小数分隔符(即,在欧洲,小数分隔符是
)。您必须添加
CultureInfo.Invariant
,以使其国际化。尽管使用
x-Math.Truncate(x)更简单
你对这个结果有魔力吗?你真的不应该用字符串来编写解决方案,或者至少把它移到答案的末尾,并注释它的缺点(或者更好地通知你,除了某些情况,这是不应该做的事情)。@Kiquenet因为这是第七次修订。这是最初的答案:这应该
double frac = value % 1;
double yourDouble = 1.05;
string stringForm = yourDouble.ToString();
int dotPosition = stringForm.IndexOf(".");
decimal decimalPart = Decimal.Parse("0" + stringForm.Substring(dotPosition));

Console.WriteLine(decimalPart); // 0.05
//will always be .02 cents
(10.02m - System.Math.Floor(10.02m))
 string input = "0.55";
    var regex1 = new System.Text.RegularExpressions.Regex("(?<=[\\.])[0-9]+");
    if (regex1.IsMatch(input))
    {
        string dp= regex1.Match(input ).Value;
    }
var float_number = 12.345;
var x = Math.IEEERemainder(float_number , 1)