web-dev-qa-db-ja.com

doubleを2つのintに分割し、1つは小数点の前、もう1つは後

Double値を、小数点の前と後の2つのint値に分割する必要があります。小数点の後の整数は2桁でなければなりません。

例:

    10.50 = 10 and 50
    10.45 = 10 and 45
    10.5  = 10 and 50
18
Scott Parker

これはあなたがそれを行う方法です:

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

文字列の操作には時間がかかる場合があります。以下を使用してみてください:

double number;

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

これを行うために使用したいプログラミング言語は何ですか?ほとんどの言語には Modulo演算子 が必要です。 C++の例:

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

文字列操作を伴わない別のバリエーション:

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

それらが小数であることを確認するか、通常の奇妙な浮動小数点/二重の動作を取得します。

2
Chris S

どう?

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

この関数は10進数で時間がかかり、基数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(); 
    }
1
DareDevil

あなたは文字列で分割してからintに変換できます...

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

試してください:

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

私は実際にこれを現実の世界で答える必要がありましたが、@ David Samuelの答えがその一部を行っていましたが、ここで私が使用した結果のコードです。前述のように、文字列はオーバーヘッドが大きすぎます。ビデオのピクセル値全体でこの計算を行う必要があり、中程度のコンピューターでも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を解決するために使用されました。

0
davidbates

あなたは文字列を経由することなくそれを行うことができます。例:

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のような番号では機能しません。繰り返しになりますが、それがあなたの表現にどのように適合するかわかりません。

0
Vlad

Linqの使用。 @Denis回答の明確化。

var splt = "10.50".Split('.').Select(int.Parse);
int i1 = splt.ElementAt(0);
int i2 = splt.ElementAt(2);
0
Maris B.