Split number into an integer and decimal

Jack

Input 23.893 would become INTEGER - 23, DECIMAL - 0.893

Here's the key snippet from my code

double realNumber = scan.nextDouble(); \\store the keyboard input in variable realNumber

double integerPart = Math.floor(realNumber); \\round down eg. 23.893 --> 23

double fractionPart = realNumber - integerPart; \\find the remaining decimal

When I try this with long numbers the decimal part differs slightly from the actual.

Input - 234.324112341234134 becomes INTEGER - 234.0,

DECIMAL - 0.3241123412341267

TechTrip

As stated by @dasblinkenlight you can use methods from BigDecimal and combine them with a splitter. I wouldn't count the decimal points though, easier to stick with BigDecimal.

For example:

public static void main(String[] args) {        
    String realNumber = "234.324823783782"; 

    String[] mySplit = realNumber.split("\\.");

    BigDecimal decimal = new BigDecimal(mySplit[0]);
    BigDecimal real = new BigDecimal(realNumber);
    BigDecimal fraction = real.subtract(decimal);

    System.out.println(String.format("Decimal : %s\nFraction: %s", decimal.toString(),fraction.toString()));

}

This outputs the following: Decimal : 234 Fraction: 0.324823783782

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Split the number on a decimal

From Dev

Regular expression to check for integer or decimal number in iOS

From Dev

Convert a decimal number that is not integer to base 4 in Matlab?

From Dev

Storing integer number to decimal slot, MATLAB

From Dev

Split Sentence if it ends with period but not number containing decimal

From Dev

Java - Split String to get decimal number

From Dev

Decimal formatting based on number of digits in integer and decimal parts

From Dev

How to split a number into sign, integer and fractional?

From Dev

How to get the last 2 digits of a number (integer or decimal)

From Dev

Distinguish between index of a decimal number and integer inside an array in Ruby?

From Dev

How can I print an integer number displaying 4 decimal digits?

From Dev

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

From Dev

Decimal to Integer in sql

From Dev

python Decimal - checking if integer

From Dev

a decimal without an integer part

From Dev

decimal representation of an integer in R

From Dev

Convert 2 decimal to integer

From Dev

Attach decimal part to an integer

From Dev

a decimal without an integer part

From Dev

convert integer into decimal

From Dev

bitstring to decimal integer conversion

From Dev

Java: how to output double-type number to integer format if its decimal is 0

From Dev

D std.random different behavior between integer and decimal uniform random number generation

From Dev

D std.random different behavior between integer and decimal uniform random number generation

From Dev

split string and convert to decimal

From Dev

How can I split a string with no delimiters but with fixed number of decimal places - python

From Dev

How can I split a string with no delimiters but with fixed number of decimal places - python

From Dev

A fixed number of decimal places with no decimal

From Dev

How can we split an integer number into a vector of its constituent digits in R

Related Related

  1. 1

    Split the number on a decimal

  2. 2

    Regular expression to check for integer or decimal number in iOS

  3. 3

    Convert a decimal number that is not integer to base 4 in Matlab?

  4. 4

    Storing integer number to decimal slot, MATLAB

  5. 5

    Split Sentence if it ends with period but not number containing decimal

  6. 6

    Java - Split String to get decimal number

  7. 7

    Decimal formatting based on number of digits in integer and decimal parts

  8. 8

    How to split a number into sign, integer and fractional?

  9. 9

    How to get the last 2 digits of a number (integer or decimal)

  10. 10

    Distinguish between index of a decimal number and integer inside an array in Ruby?

  11. 11

    How can I print an integer number displaying 4 decimal digits?

  12. 12

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

  13. 13

    Decimal to Integer in sql

  14. 14

    python Decimal - checking if integer

  15. 15

    a decimal without an integer part

  16. 16

    decimal representation of an integer in R

  17. 17

    Convert 2 decimal to integer

  18. 18

    Attach decimal part to an integer

  19. 19

    a decimal without an integer part

  20. 20

    convert integer into decimal

  21. 21

    bitstring to decimal integer conversion

  22. 22

    Java: how to output double-type number to integer format if its decimal is 0

  23. 23

    D std.random different behavior between integer and decimal uniform random number generation

  24. 24

    D std.random different behavior between integer and decimal uniform random number generation

  25. 25

    split string and convert to decimal

  26. 26

    How can I split a string with no delimiters but with fixed number of decimal places - python

  27. 27

    How can I split a string with no delimiters but with fixed number of decimal places - python

  28. 28

    A fixed number of decimal places with no decimal

  29. 29

    How can we split an integer number into a vector of its constituent digits in R

HotTag

Archive