Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get all decimal places in a number after division

I am currently using the BigDecimal and it is giving me more decimals but not nearly enough for what I am trying to do. I need to be able to get all the way to the 10^6 digit. This is my current code

BigDecimal num = new BigDecimal(103993/33102.0);
    pw.println(num.toString());

and it outputs 3.14159265301190249175533608649857342243194580078125

where the number actually has a lot more decimals: http://www.wolframalpha.com/input/?i=103993%2F33102

like image 237
Will Jamieson Avatar asked Mar 01 '13 18:03

Will Jamieson


People also ask

How do you find the value of the decimal after division?

For example, if we need to divide 54.79 ÷ 1.5, we will multiply the divisor by 10 to remove the decimal point. this will be 1.5 × 10 = 15. Now, we need to multiply the dividend with the same number 10, so we get, 54.79 × 10 = 547.9.

Can you get decimals with long division?

We can use the long division process to work out the answer to a number of decimal places. The secret to working out a long division to decimal places is the ability to add zeros after the decimal point. We can add as many zeros as we wish after the decimal point without altering the numbers value.

How do you extract the decimal part of a number in Python?

modf() function is an inbuilt function in Python that returns the fractional and integer parts of the number in a two-item tuple. Both parts have the same sign as the number. The integer part is returned as a float.


1 Answers

You are loosing the precision when evaluating this:

103993/33102.0

as a double division. Actually, the following:

BigDecimal num = new BigDecimal(103993/33102.0);

is equivlent to:

double d = 103993/33102.0;
BigDecimal num = new BigDecimal(d);

instead, use:

int scale = 100;
BigDecimal num1 = new BigDecimal(103993);
BigDecimal num2 = new BigDecimal(33102);
System.out.println(num1.divide(num2, scale, RoundingMode.HALF_UP).toString());

OUTPUT:

3.1415926530119026040722614947737296840070086399613316415926530119026040722614947737296840070086399613316415926530119026040722614947737296840070086399613316415926530119026040722614947737296840070086399613316415926530119026040722614947737296840070086399613316415926530119026040722614947737296840070086399613316415926530119026040722614947737296840070086399613316415926530119026040722614947737296840070086399613316415926530119026040722614947737296840070086399613316415926530119026040722614947737296840070086399613316415926530119026040722614947737296840070086399613316415926530119026040722614947737296840070086399613316415926530119026040722614947737296840070086399613316415926530119026040722614947737296840070086399613316415926530119026040722614947737296840070086399613316415926530119026040722614947737296840070086399613316415926530119026040722614947737296840070086399613316415926530119026040722614947737296840070086399613316415926530119026040722614947737296840070086399613316415926530119026040722614947737
like image 111
Eng.Fouad Avatar answered Oct 17 '22 23:10

Eng.Fouad