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.

mikeLspohn
  • 495
  • 1
  • 8
  • 21
  • I think it's %.3f, give me a minute to check – msknapp Sep 08 '14 at 03: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. – msknapp Sep 08 '14 at 04: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. – mikeLspohn Sep 08 '14 at 14:55

1 Answers1

0

Try this:

System.out.printf("The Sqrt Rounded Down Is: " + "%.3f", Math.floor(Math.sqrt(hi)));
Óscar López
  • 232,561
  • 37
  • 312
  • 386
  • 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. – mikeLspohn Sep 08 '14 at 03:59
  • It was my pleasure :) – Óscar López Sep 08 '14 at 04:05