This question already has answers here:
is there anyway to convert from Double to BigInteger?
(3 answers)
Closed 5 years ago.
How do you convert a double to a BigInteger, as accurately as possible? BigInteger doesn't have a valueOf(double) method. It does have a valueOf(long) method, but converting via long, would destroy any value outside the 2^64 range, and in particular I'm trying to get large numbers like 1e100 to convert to their corresponding integer values to the sixteen or so significant figures allowed by floating point precision.
You can convert the double into a BigDecimal and then into a BigInteger:
BigInteger k = BigDecimal.valueOf(doublevalue).toBigInteger();
You could use BigDecimal.valueOf(double) and then call toBigIntegerExact() on that. Like,
BigInteger bi = BigDecimal.valueOf(Math.PI).toBigIntegerExact();
Related
This question already has answers here:
BigDecimal not keeping actual value when being returned from Java method
(2 answers)
Closed 12 months ago.
I am not sure if I am using this wrong but, if I do BigDecimal.valueOf(15350.00), why does it show as 15350.0 instead of 15350.00?
When working with BigDecimal, prefer using the BigDecimal(String) constructor over valueOf(double). new BigDecimal("15350.00") will preserve your trailing zeroes.
Although 15350.00 and 15350.0 are two different BigDecimal values, BigDecimal.valueOf(15350.00) is constructing the BigDecimal using a Double. The double literals 15350.00 and 15350.0 both correspond to the same exact integer value, without the precision semantics of BigDecimal.
This question already has answers here:
Beginners Java Question (int, float)
(4 answers)
Division of integers in Java [duplicate]
(7 answers)
Closed 2 years ago.
If I divide a double type variable, the Decimal part becomes zero.
a=13122/10;
System.out.println (a);
Prints
1312.0
As you can see, the Decimal part became zero when I divided it.
But I need the value
1312.2
Your problem is that while you may have stored "a" as a double, you are really dividing two "ints" and saving that. When you divide 2 ints, the number automatically gets rounded down. So, it's rounded down to 1312.0.
What you need is this,
a = (double)13122/10;
or this:
a = 13122.0/10;
You are dividing integers. You can cast the division and then you are ok
double a = (double) 13122/10;
System.out.println (a);
This question already has answers here:
How to round a number to n decimal places in Java
(39 answers)
Closed 3 years ago.
I want to take a float that can be entered by the user to any acceptable number of decimal places and format it so that it is only two decimal places. I am able to do this by converting it to a String but I want to keep it as a float and was wondering if there was an easier way to do this than changing from float to string and then back to a float?
Here's a way to do it:
https://stackoverflow.com/a/153785/4119650
It's a little more complicated than the String conversion method, but it gives you greater control over the truncation.
This question already has answers here:
Datatype to store 20 digit number
(4 answers)
Closed 6 years ago.
I am trying to convert 10000000000000000000000000000000 to BigInteger.
But it is not converting.
My code is
BigInteger number = BigInteger.valueOf(10000000000000000000000000000000);
It is showing The literal 10000000000000000000000000000000 of type int is out of range.
Is there any way to convert this number as I have to use this number as integer in my programme?
The whole point of BigInteger is to support numbers long and int cannot so you can't use those. What you can do is use a String.
BigInteger bi = new BigInteger("10000000000000000000000000000000"):
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
How to do a fractional power on BigDecimal in Java?
I have a BigInteger A that I need to exponentiate with 1/b (b is an int).
My problem is that A supports only A.pow(int) which is not suitable for my case.
Is there any workaround for this?
Newton's method is your jam.
because BigInteger are intended for Rational numbers, instead fractional powers are Real numbers.
http://en.wikipedia.org/wiki/Real_number
The workaround is using a temporary float and then approximate to the nearest BigDecimal.