我需要将一个double值拆分为两个int值,一个在小数点之前,一个在之后。小数点后的int应该有两位数。
示例:
10.50 = 10 and 50
10.45 = 10 and 45
10.5 = 10 and 50
答案 0 :(得分:24)
你可以这样做:
string s = inputValue.ToString("0.00", CultureInfo.InvariantCulture);
string[] parts = s.Split('.');
int i1 = int.Parse(parts[0]);
int i2 = int.Parse(parts[1]);
答案 1 :(得分:16)
操纵字符串可能很慢。请尝试使用以下内容:
double number;
long intPart = (long) number;
double fractionalPart = number - intPart;
答案 2 :(得分:7)
您要使用哪种编程语言来执行此操作?大部分语言都应该有Modulo operator。 C ++示例:
double num = 10.5;
int remainder = num % 1
答案 3 :(得分:4)
"10.50".Split('.').Select(int.Parse);
答案 4 :(得分:3)
/// <summary>
/// Get the integral and floating point portions of a Double
/// as separate integer values, where the floating point value is
/// raised to the specified power of ten, given by 'places'.
/// </summary>
public static void Split(Double value, Int32 places, out Int32 left, out Int32 right)
{
left = (Int32)Math.Truncate(value);
right = (Int32)((value - left) * Math.Pow(10, places));
}
public static void Split(Double value, out Int32 left, out Int32 right)
{
Split(value, 1, out left, out right);
}
用法:
Int32 left, right;
Split(10.50, out left, out right);
// left == 10
// right == 5
Split(10.50, 2, out left, out right);
// left == 10
// right == 50
Split(10.50, 5, out left, out right);
// left == 10
// right == 50000
答案 5 :(得分:2)
另一种不涉及字符串操作的变体:
static void Main(string[] args)
{
decimal number = 10123.51m;
int whole = (int)number;
decimal precision = (number - whole) * 100;
Console.WriteLine(number);
Console.WriteLine(whole);
Console.WriteLine("{0} and {1}",whole,(int) precision);
Console.Read();
}
确保它们是小数,或者你得到通常奇怪的浮动/双重行为。
答案 6 :(得分:1)
你可以用字符串拆分然后转换成int ...
string s = input.ToString();
string[] parts = s.Split('.');
答案 7 :(得分:1)
此函数将花费十进制时间并转换回基数60。
public string Time_In_Absolute(double time)
{
time = Math.Round(time, 2);
string[] timeparts = time.ToString().Split('.');
timeparts[1] = "." + timeparts[1];
double Minutes = double.Parse(timeparts[1]);
Minutes = Math.Round(Minutes, 2);
Minutes = Minutes * (double)60;
return string.Format("{0:00}:{1:00}",timeparts[0],Minutes);
//return Hours.ToString() + ":" + Math.Round(Minutes,0).ToString();
}
答案 8 :(得分:0)
尝试:
string s = "10.5";
string[] s1 = s.Split(new char[] { "." });
string first = s1[0];
string second = s1[1];
答案 9 :(得分:0)
你可以不经过字符串就可以做到。例如:
foreach (double x in new double[]{10.45, 10.50, 10.999, -10.323, -10.326, 10}){
int i = (int)Math.Truncate(x);
int f = (int)Math.Round(100*Math.Abs(x-i));
if (f==100){ f=0; i+=(x<0)?-1:1; }
Console.WriteLine("("+i+", "+f+")");
}
输出:
(10, 45)
(10, 50)
(11, 0)
(-10, 32)
(-10, 33)
(10, 0)
但是对-0.123
这样的数字不起作用。然后,我不确定它是如何适合你的代表。
答案 10 :(得分:0)
我实际上只是必须在现实世界中回答这个问题,而@David Samuel的回答在这里做了部分原因是我使用的结果代码。如前所述,Strings的开销太大了。我不得不对视频中的像素值进行此计算,并且仍能够在中等计算机上保持30fps。
double number = 4140 / 640; //result is 6.46875 for example
int intPart = (int)number; //just convert to int, loose the dec.
int fractionalPart = (int)((position - intPart) * 1000); //rounding was not needed.
//this procedure will create two variables used to extract [iii*].[iii]* from iii*.iii*
这用于解决640 X 480视频输入中像素数的x,y。
答案 11 :(得分:0)
使用Linq。只是澄清了@Denis的答案。
var splt = "10.50".Split('.').Select(int.Parse);
int i1 = splt.ElementAt(0);
int i2 = splt.ElementAt(2);
答案 12 :(得分:0)
怎么样?
var n = 1004.522
var a = Math.Floor(n);
var b = n - a;
答案 13 :(得分:0)
Console.Write("Enter the amount of money: ");
double value = double.Parse(Console.ReadLine());
int wholeDigits = (int) value;
double fractionalDigits = (value - wholeDigits) * 100;
fractionalDigits = (int) fractionalDigits;
Console.WriteLine(
"The number of the shekels is {0}, and the number of the agurot is {1}",
wholeDigits, fractionalDigits);