0

I've read a few other answers on different posts about people having specific problems with line formatting, but have not found anything that has helped enough to solve my problem. I've also looked at the java docs and nothing I have done seems to work.

The last line of code is the one I'm trying to format. I ideally want to format it with 3 decimal precision output.

Double hi = 32.22;
System.out.println("The sqrt. is = " + Math.sqrt(hi));
System.out.printf("The Sqrt Rounded Down Is: " + "/%.3d", Math.floor(Math.sqrt(hi)));

is anything wrong with me "/%.3d", or is it something else? I don't quite fully understand/remember all the formatting options.

3
  • I think it's %.3f, give me a minute to check Commented Sep 8, 2014 at 3:56
  • I see Mike beat me to it, I stopped to think, "why is he adding decimals after a number that should clearly be an integer?" I mean java will have it stored as a double, but it will be a round number. Why add zeros? I started thinking you might have meant to prefix it (aka pad it) with zeros instead. Commented Sep 8, 2014 at 4:03
  • I'm just learning the Math functions, and the formatting numbers with printf so it was ideally just a learning exercise. I can see definitely see why you would wonder why I would want to do that though. Commented Sep 8, 2014 at 14:55

1 Answer 1

0

Try this:

System.out.printf("The Sqrt Rounded Down Is: " + "%.3f", Math.floor(Math.sqrt(hi)));
Sign up to request clarification or add additional context in comments.

2 Comments

Yes, thank you very much. I'm not sure why I was using a d, I thought I had literally just seen d for doubles in the docs but I must've been mistaken. Stupid mistakes happen learning new things occasionally.
It was my pleasure :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.