我想以整数形式接收小数点后的数字。例如,从1.05开始只有05或从2.50开始只有05,而不是0.50
decimal,float,string,...?
我想以整数形式接收小数点后的数字。例如,从1.05开始只有05或从2.50开始只有05,而不是0.50
decimal,float,string,...?
Answers:
更新的答案
在这里,我给出了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]使用乘数[是N的幂的10(例如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的更多信息
.,该并不总是十进制分隔符(即在欧洲,十进制分隔符为,)。您必须添加CultureInfo.Invariant使其国际化。尽管使用起来更容易x - Math.Truncate(x),但您会对结果感到惊讶。
最好的最好方法是:
var floatNumber = 12.5523;
var x = floatNumber - Math.Truncate(floatNumber);
结果你可以随意转换
没有舍入问题的解决方案:
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。
int last2digits = num - (int) ((double) (num / 100) * 100);
318.401567d解决方案输出0.401567,其中40的预期。
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;
}
这是我为类似情况编写的扩展方法。我的应用程序将接收格式为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);
}
}
您可以.从将双精度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
使用正则表达式:Regex.Match("\.(?\d+)")如果我在这里错了,请有人纠正我
Regex.Match但尝试获取小数点的值时收到以下错误ArgumentException was unhandled: parsing "\.(?\d+)" - Unrecognized grouping construct.
很简单
float moveWater = Mathf.PingPong(theTime * speed, 100) * .015f;
int m = (int)(moveWater);
float decimalPart= moveWater -m ;
Debug.Log(decimalPart);