如何在Java中按点将双数拆分为两个十进制数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24753177/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 14:41:41  来源:igfitidea点击:

How to split a double number by dot into two decimal numbers in Java?

java

提问by Johnny Chen

Trying to split a double number into two decimal parts by dot. Like this: 1.9 into 1 and 9; 0.16 into 0 and 16;

试图通过点将双数分成两个小数部分。像这样:1.9 变成 1 和 9;0.16 变成 0 和 16;

Here's what I do, but seems a little redundant, what's the best way to do this?

这是我所做的,但似乎有点多余,这样做的最佳方法是什么?

The origin number will always be like Just 0.x or 1.x or 0.xx or 1.xx and xx > 10

原始编号将始终类似于 Just 0.x or 1.x or 0.xx or 1.xx and xx > 10

    double d = 1.9;
    int a, b;
    String dString = Double.toString(d);
    String aString = dString.substring(0, 1);

    String bString = dString.substring(2);
    a = Integer.parseInt(aString);
    b = Integer.parseInt(bString);

My way of doing this seems using to much String conversion,which I don't think is very efficient.

我这样做的方式似乎使用了很多字符串转换,我认为这不是很有效。

采纳答案by Ruchira Gayan Ranaweera

You can try this way too

你也可以试试这个方法

    double val=1.9;
    String[] arr=String.valueOf(val).split("\.");
    int[] intArr=new int[2];
    intArr[0]=Integer.parseInt(arr[0]); // 1
    intArr[1]=Integer.parseInt(arr[1]); // 9

回答by Keppil

To get the decimal part as an intis not really following any standards, so you are stuck with your special solution for that. Getting the value before the decimal point can be simplified to a simple cast though:

将小数部分作为 anint并没有真正遵循任何标准,因此您坚持使用您的特殊解决方案。获取小数点前的值可以简化为简单的转换:

double d = 1.9;
int a, b;
String dString = Double.toString(d);
String bString = dString.substring(2);
a = (int) d;
b = Integer.parseInt(bString);

Note that the substring()and parseInt()fails if the number is 10 or bigger though. You might want to change the substring()call to something like:

请注意,如果数字为 10 或更大,则substring()parseInt()失败。您可能希望将substring()调用更改为:

String bString = dString.split("\.")[1];

回答by tudoricc

I tried a split on "." and then just parse the string for integers.

我尝试对“.”进行拆分。然后只是解析字符串的整数。

        String a="";
        double x = 2.4;
        a=x+"";
        String [] v =a.split("\.");
        int before = Integer.parseInt(v[0]);
        int after = Integer.parseInt(v[1]);

回答by nobalG

double num=12.5;
String str=Double.toString(num);
String strarray[]=str.split("\.");

Now strarray[0]will be holding 12, and strarray[1]will be having 5.You can convert them into integers by writing following code:

现在strarray[0]将持有 12,并将strarray[1]拥有 5。您可以通过编写以下代码将它们转换为整数:

int num1=Integer.parseInt(strarray[0]);
int num2=Integer.parseInt(strarray[1]);

回答by JamesA

You could treat the double like a string, and split it on the decimal place.

您可以将 double 视为字符串,并将其拆分到小数位。

     double d = 13454.92345;
     String bob = Double.toString(d);
     String[] convert = bob.split("\.");

     int a = Integer.parseInt(convert[0]);
     int b = Integer.parseInt(convert[1]);

     System.out.println(a); // 13454
     System.out.println(b); // 92345

回答by Bohemian

Here's a two line version:

这是一个两行版本:

int a = (int)d;
int b = Integer.parseInt((d + "").split("\.", 2)[1]).replaceAll("^$", "0"));

The first part is easy - just cast to int, which automatically chops off any decimal part of the number.

第一部分很简单 - 只需将int其强制转换为,它会自动切掉数字的任何小数部分。

The second part is easiest with a Strimg approach - split the string version on a dot and use the second part. Note the addition of ""which generates a string, and the handling of the edge case of there being no decimal part, where 3as a double prints as 3.- if the result is a blank (guaranteed by the second parameter to split) which is then converted to a zero.

使用 Strimg 方法,第二部分是最简单的 - 在一个点上拆分字符串版本并使用第二部分。注意它的添加""会生成一个字符串,以及处理没有小数部分的边缘情况,其中3双打印为3.- 如果结果是空白(由要拆分的第二个参数保证),然后将其转换为一个零。

回答by bumbumpaw

  double d = 1.9;
  String str = Double.toString(d);
  String strArray[] = str.split("\.");

  int a = Integer.parseInt(strArray[0]);
  int b = Integer.parseInt(strArray[1]);
  System.out.print(a + " - " + b);

Try this. This will work.

尝试这个。这将起作用。

回答by Patrik Oldsberg

This solution should work for almost any length of the fractional part and doesn't use strings. For example the accepted answer doesn't work for 0.16

此解决方案几乎适用于小数部分的任何长度,并且不使用字符串。例如,接受的答案不适用于 0.16

double d = 123.456

long a = (long) d;
double f = d - a;

while (Math.abs((long) f - f) > 0.000001) f *= 10;

long b = (long) f;

// a = 123
// b = 345

回答by Richard Mu?oz

Here is another approach:

这是另一种方法:

double d=1.9;
int a;
int b;

a = (int) d/10;
b = (int) d%10;

System.out.print("a");
System.out.print("b");

回答by Loyola

    double dub=1234.5678987;//Variable to be manipulated
    long lon=0; //To store before decimal point.    
    short sh=0;//To store after decimal point.  
    int deci=10000; //to 4 decimal places   
    lon=(int)dub;// get the integer part of the number stored in long   
    sh=(short)((dub-(double)lon)*deci);//deci to required decimal places.
    System.out.println(""+lon+"."+sh);