BigDecimal x = new BigDecimal(1.95);
BigDecimal y = new BigDecimal(0.65);
BigDecimal rem = x.remainder(y);
if (rem.compareTo(BigDecimal.ZERO) != 0.) {
System.out.println("Not In mutilple of");
} else {
System.out.println("In mutilple of");
}
System.out.println(rem);
Not giving correct result for the above scenario. given the input condition but giving incorrect result if it is in multiple of the second value
Правильный результат должен быть кратен, потому что моя цель - проверить, что 1,95 кратно 0,65.
Создать как
BigDecimal x= new BigDecimal("1.95");
BigDecimal y= new BigDecimal("0.65");
поскольку не все числа с плавающей запятой могут быть представлены точно как double
s.
Вывод в вашем коде показывает
0.65
СС 0.64999999999999995559107901499373838305473327636718750
см. https://study.com/academy/lesson/java-floating-point-numbers.html
Использование new BigDecimal(double)
может привести к потере точности, см. док:
The results of this constructor can be somewhat unpredictable. One might assume that writing new BigDecimal(0.1) in Java creates a BigDecimal which is exactly equal to 0.1 (an unscaled value of 1, with a scale of 1), but it is actually equal to 0.1000000000000000055511151231257827021181583404541015625. This is because 0.1 cannot be represented exactly as a double (or, for that matter, as a binary fraction of any finite length). Thus, the value that is being passed in to the constructor is not exactly equal to 0.1, appearances notwithstanding.
и:
BigDecimal x= new BigDecimal(1.95);
BigDecimal y = new BigDecimal(0.65);
System.out.println(x); // 1.9499999999999999555910790149937383830547332763671875
System.out.println(y); // 0.65000000000000002220446049250313080847263336181640625
Используйте BigDecimal(String val)
:
The String constructor, on the other hand, is perfectly predictable: writing new BigDecimal("0.1") creates a BigDecimal which is exactly equal to 0.1, as one would expect. Therefore, it is generally recommended that the String constructor be used in preference to this one.
и
BigDecimal x = new BigDecimal("1.95");
BigDecimal y = new BigDecimal("0.65");
System.out.println(x); // 1.95
System.out.println(y); // 0.65
System.out.println(x.remainder(y)); // 0.00
Строго говоря, точность теряется во время компиляции ... когда литерал преобразуется в значение с плавающей запятой IEE.
Тогда это даст мне неверный результат для немасштабированного значения, которое я получаю.
@TaherMahodia - Что вы имеете в виду - пожалуйста, дайте образец
@Scary Wombat Он также дает неверный результат для немасштабированного значения BigDecimal x = new BigDecimal (1.94999999999999995559107901499373838305473327636 71875); BigDecimal y = новый BigDecimal (0,65000000000000002220446049250313080847263336181 640625);
Я хочу проверить, что два больших десятичных числа кратны или нет
так что посмотри мой ответ
Кстати, на машине @Sun / OS / Версия Java 0.65
становится 0.65000000000000002220446049250313080847263336181640625
на моем испытательном стенде, он становится 0.64999999999999995559107901499373838305473327636718750
Каков правильный результат?