V
V
Vadim Gush2014-01-28 10:11:10
Java
Vadim Gush, 2014-01-28 10:11:10

How to write only two decimal places?

I have three numbers that look like this:
x = 0.154848784
y = 0.88787754
z = 0.4848484455
At the same time, during the execution of the program, these numbers change and the number of decimal places too, I need the program to write to me only 2 decimal places of these values.
For example: x = 0.154848784 and in the console it should write x = 0.15

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Gornostaev, 2014-01-28
@VadimGu

String.format("%.2f", x);

S
Sergei Borisov, 2014-01-28
@risik

How to print has already been explained to you. And here is the answer to the "why" question: Float in C, 0.41 is actually 0.40999

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question