-1

Code

I wanted the output to be 0.123456789123456789. But, result is 0.123456789 in float type. 0.12345678912345678 in double type. So, I tried using %030.19f,e but this doesn't work either. How do I output 0.123456789123456789?

lloydpick
  • 1,638
  • 1
  • 18
  • 24
  • 1
    Please post the relevant part of your code (properly formatted) into your question as text. A screenshot of your code is not helpful. – khelwood Mar 29 '18 at 12:40
  • Please check [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) – iksajotien Mar 29 '18 at 12:47
  • Sorry, I'll remember that now on. – Yang Gyeonghwan Mar 29 '18 at 12:57
  • Possible duplicate of [How many significant digits have floats and doubles in java?](https://stackoverflow.com/questions/13542944/how-many-significant-digits-have-floats-and-doubles-in-java) – rghome Mar 29 '18 at 13:41

2 Answers2

1

For bigger numbers you can use BigDecimal. For example:

import java.math.BigDecimal;

public class HelloWorld{

 public static void main(String []args){
    BigDecimal bdcm=new BigDecimal("0.123456789123456789");
    System.out.println(bdcm);
    BigDecimal bdcm2=new BigDecimal("100.123456789123456789");
    System.out.println(bdcm.add(bdcm2));
 }
}

And the output is proper :)

Veselin Davidov
  • 7,031
  • 1
  • 15
  • 23
0

Don't use float or double if you need to print exact values.

Use instead a BigDecimal that is a class to hold

Immutable, arbitrary-precision signed decimal numbers

The problem using float and double is that they suffer for the rounding related to binary decimal numbers.

Davide Lorenzo MARINO
  • 26,420
  • 4
  • 39
  • 56
  • Thanks you. decimal numbers that i told exceed limit of float and double. I understand right? – Yang Gyeonghwan Mar 29 '18 at 13:15
  • Yes and not only. It is possible that also a relative little decimal number can't be represented with a float (or double) – Davide Lorenzo MARINO Mar 29 '18 at 13:20
  • Re “ if you need to print exact values”: The difference between `double` and `BigDecimal` is not that one is exact and the other is inexact. It is that one is binary and fixed precision and the other is decimal and variable precision. `BigDecimal` can represent decimal numerals exactly, but it cannot represent other numbers exactly. For example, it does not represent one-third exactly. – Eric Postpischil Mar 29 '18 at 13:41
  • @EricPostpischil you are right. But here we are talking about decimal numbers. – Davide Lorenzo MARINO Mar 29 '18 at 13:44
  • @DavideLorenzoMARINO: Are you sure OP is going to do nothing other than read and write numbers, perhaps do a little addition or simple multiplication? Because if they do almost any other arithmetic, they will get rounding errors, which they will not expect because you have told them `BigDecimal` is exact. – Eric Postpischil Mar 29 '18 at 13:47
  • @EricPostpischil using BigDecimal you have full control over the rounding due to math operations. So is you that decide how the result is rounded after an operation choosing the precision, scale and rounding mode. – Davide Lorenzo MARINO Mar 29 '18 at 13:53