我想以整数的形式接收小数点后的数字。例如,只有 05 来自 1.05 或来自 2.50 只有 50而不是0.50
19 回答
最好的方法是:
var floatNumber = 12.5523;
var x = floatNumber - Math.Truncate(floatNumber);
结果你可以随心所欲地转换
var decPlaces = (int)(((decimal)number % 1) * 100);
这假定您的数字只有两位小数。
有一个比“Math.Truncate”方法更干净、更快捷的解决方案:
double frac = value % 1;
无舍入问题的解决方案:
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。
更好的方法 -
double value = 10.567;
int result = (int)((value - (int)value) * 100);
Console.WriteLine(result);
输出 -
56
最简单的变体可能是 Math.truncate()
double value = 1.761
double decPart = value - Math.truncate(value)
我猜这个帖子已经老了,但我不敢相信没有人提到过 Math.Floor
//will always be .02 cents
(10.02m - System.Math.Floor(10.02m))
var result = number.ToString().Split(System.Globalization.NumberDecimalSeparator)[2]
将其作为字符串返回(但您始终可以将其转换回 int),并假定该数字确实有一个“。” 某处。
int last2digits = num - (int) ((double) (num / 100) * 100);
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;
}
string input = "0.55";
var regex1 = new System.Text.RegularExpressions.Regex("(?<=[\\.])[0-9]+");
if (regex1.IsMatch(input))
{
string dp= regex1.Match(input ).Value;
}
var d = 1.5;
var decimalPart = Convert.ToInt32(d.ToString().Split('.')[1]);
它给你5
来自1.5
:)
.
您可以在将双精度转换为字符串后从您尝试使用函数获取小数的双精度中删除点,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
这是我为类似情况编写的扩展方法。我的应用程序将收到 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 答案慢 3-4 倍,但只有一个函数调用。也许有人喜欢它:
var float_number = 12.345;
var x = Math.IEEERemainder(float_number , 1)
使用正则表达式:Regex.Match("\.(?\d+)")
如果我在这里错了,请有人纠正我
这很简单
float moveWater = Mathf.PingPong(theTime * speed, 100) * .015f;
int m = (int)(moveWater);
float decimalPart= moveWater -m ;
Debug.Log(decimalPart);
为什么不使用int y = value.Split('.')[1];
?
该Split()
函数将值拆分为单独的内容,然后1
输出第二个值.
更新的答案
在这里,我给出了 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 的更多信息