bigdecimal

Java BigDecimal difference

两盒软妹~` 提交于 2021-01-28 03:03:36
问题 I wanted to see if anyone can explain why the following code works with valueOf but not others. import java.math.BigDecimal; public class Change { public static void main(String args[]) { double a = 4.00d; double b = 3.10d; BigDecimal a1 = new BigDecimal(a); BigDecimal b1 = new BigDecimal(b); BigDecimal diff = a1.subtract(b1); System.out.println("Double difference"); System.out.println(diff); float c = 4.00f; float d = 3.10f; BigDecimal a2 = new BigDecimal(c); BigDecimal b2 = new BigDecimal(d

Java BigDecimal difference

爷,独闯天下 提交于 2021-01-28 00:33:18
问题 I wanted to see if anyone can explain why the following code works with valueOf but not others. import java.math.BigDecimal; public class Change { public static void main(String args[]) { double a = 4.00d; double b = 3.10d; BigDecimal a1 = new BigDecimal(a); BigDecimal b1 = new BigDecimal(b); BigDecimal diff = a1.subtract(b1); System.out.println("Double difference"); System.out.println(diff); float c = 4.00f; float d = 3.10f; BigDecimal a2 = new BigDecimal(c); BigDecimal b2 = new BigDecimal(d

How to set specific precision to BigDecimal in Java?

淺唱寂寞╮ 提交于 2020-12-16 03:54:52
问题 I have BigDecimal value. BigDecimal price = new BigDecimal("100500100500.9999999").setScale(2, BigDecimal.ROUND_HALF_EVEN); It prints 100500100501.00 , although I need 100500100500.99 . Is it possible to make some restriction to precision evaluating? 回答1: You can use the rounding mode constant ROUND_DOWN in setScale . However, the overloaded setScale method that takes a RoudingMode is preferred. BigDecimal price = new BigDecimal("100500100500.9999999") .setScale(2, RoundingMode.DOWN); Output:

How to set specific precision to BigDecimal in Java?

亡梦爱人 提交于 2020-12-16 03:52:45
问题 I have BigDecimal value. BigDecimal price = new BigDecimal("100500100500.9999999").setScale(2, BigDecimal.ROUND_HALF_EVEN); It prints 100500100501.00 , although I need 100500100500.99 . Is it possible to make some restriction to precision evaluating? 回答1: You can use the rounding mode constant ROUND_DOWN in setScale . However, the overloaded setScale method that takes a RoudingMode is preferred. BigDecimal price = new BigDecimal("100500100500.9999999") .setScale(2, RoundingMode.DOWN); Output:

How to convert only the integer part of a BigDecimal to hex string?

假装没事ソ 提交于 2020-12-06 07:09:19
问题 I have a number of type BigDecimal, for example 18446744073709551616, and I want to convert it to hexadecimal value. I'm fine with truncating the fractional portion. Is there a way to do this instead of doing it manually? 回答1: Judging by your example you should use BigInteger instead of BigDecimal . This way you could use new BigInteger("18446744073709551616").toString(16) If you can't change type of original object convert it to BigInteger later in method new BigDecimal("18446744073709551616

How to get integer and fraction portions of a BigDecimal in Java

荒凉一梦 提交于 2020-08-09 19:33:23
问题 For a BigDecimal number such as 42.7, how can I tear this apart to get the integer part (42) and the decimal fractional part (0.7)? I would like to retrieve both as BigDecimal objects. But I will take what I can get and make BigDecimal object from them. 回答1: setScale() I think I’d go for setScale() . BigDecimal bd = new BigDecimal("42.7"); BigDecimal integerPart = bd.setScale(0, RoundingMode.DOWN); BigDecimal fractionalPart = bd.subtract(integerPart); System.out.println(integerPart); System

How to get integer and fraction portions of a BigDecimal in Java

别来无恙 提交于 2020-08-09 19:32:25
问题 For a BigDecimal number such as 42.7, how can I tear this apart to get the integer part (42) and the decimal fractional part (0.7)? I would like to retrieve both as BigDecimal objects. But I will take what I can get and make BigDecimal object from them. 回答1: setScale() I think I’d go for setScale() . BigDecimal bd = new BigDecimal("42.7"); BigDecimal integerPart = bd.setScale(0, RoundingMode.DOWN); BigDecimal fractionalPart = bd.subtract(integerPart); System.out.println(integerPart); System

BigDecimal with smart precision (scale) [duplicate]

假如想象 提交于 2020-07-31 04:52:03
问题 This question already has answers here : Removing trailing zeros from BigDecimal in Java (6 answers) Closed 2 years ago . How to set generic scaling according to decimal roundness as a number BigDecimal num = new BigDecimal(25) BigDecimal result = num.divide(new BigDecimal(13), 7, RoundingMode.HALF_EVEN) // 1.9230769 but BigDecimal result = num.divide(new BigDecimal(10), 7, RoundingMode.HALF_EVEN) // 2.5000000 that should be 2.5 Probably analyzing remainder? Is there something in the API for