将双精度拆分为两个整数,一个小数点之前一个整数,一个小数点之后一个整数

本文关键字:一个 整数 小数点 之后 两个 双精度 拆分 | 更新日期: 2023-09-27 18:13:41

我需要将一个双精度值拆分为两个整数值,一个在小数点之前,一个在小数点之后。小数点后的 int 应有两位数。

例:

    10.50 = 10 and 50
    10.45 = 10 and 45
    10.5  = 10 and 50

将双精度拆分为两个整数,一个小数点之前一个整数,一个小数点之后一个整数

这是你可以做到的:

string s = inputValue.ToString("0.00", CultureInfo.InvariantCulture);
string[] parts = s.Split('.'); 
int i1 = int.Parse(parts[0]);
int i2 = int.Parse(parts[1]);

操作字符串可能会很慢。尝试使用以下方法:

double number;
long intPart = (long) number;
double fractionalPart = number - intPart;

你想用什么编程语言来做这件事?大多数语言都应该有一个 Modulo 运算符。C++示例:

double num = 10.5;
int remainder = num % 1
"10.50".Split('.').Select(int.Parse);
/// <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

怎么样?

var n = 1004.522
var a = Math.Floor(n);
var b = n - a;

另一个不涉及字符串操作的变体:

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();
}

确保它们是小数,否则你会得到通常的奇怪浮动/双重行为。

你可以用字符串拆分,然后转换为int ...

string s = input.ToString(); 
string[] parts = s.Split('.');

此函数将以十进制计算时间并转换回以 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(); 
    }

使用 Linq。只是澄清@Denis答案。

var parts = "10.50".Split('.').Select(int.Parse);
int i1 = parts.ElementAt(0);
int i2 = parts.ElementAt(1);

尝试:

string s = "10.5";
string[] s1 = s.Split(new char[] { "." });
string first = s1[0];
string second = s1[1];

您可以在不通过字符串的情况下执行此操作。例:

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这样的数字。再说一次,我不确定它如何适合您的表示。

我实际上只需要在现实世界中回答这个问题,而塞缪尔@David答案在这里做了一部分是我使用的结果代码。如前所述,字符串的开销太大了。我必须对视频中的像素值进行此计算,并且仍然能够在中等计算机上保持 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。

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);