How to form a number to have 10 decimal places?

Asked

Viewed 423 times

-1

I have a sum operation and have to display the result in a number with 10 decimal places of accuracy. Any idea?

  • 1

    What have you tried? Add your code to the question,

  • 1

    It depends on what that number is. It’s a monetary value or something?

  • is a float resulting from splitting 2 int.

  • Splitting 2 int does not result in a float type. Please add your code by editing the question, to make it easier for us to understand its difficulty.

2 answers

1

 String value = String.format("Dez casas decimais: %1$.10f",10/3d);
 System.out.println(value);
  • 1

    Nice answer Rovann, but it would be interesting to explain a little your code so that it is more clear.

  • is a normal float, result of splitting 2 int.

1


We use "f" to indicate a floating point. Before that, we specify ". 10" to mean "Ten numbers after decimal." example - ideone

    double number = 1.23456789;
    String value = String.format("Dez números após o decimal: %1$.10f",
            number);
    System.out.println(value);

Examples using sum of two numbers:

example - ideone

double number = 1.11111111111111111+1.11111111111111111;
String value = String.format("Dez números após o decimal: %1$.10f",
        number);
System.out.println(value);

example - ideone

    String result = String.format("%.10f", 10.3333333333333333 + 3.33333333333333333);
    System.out.println (result);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.