• Post Reply Bookmark Topic Watch Topic
  • New Topic

Printing number of digits after decimal places using print function  RSS feed

 
Nikhil Nair
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



I have written this small code to print square root as well as quadratic root.Is their a way to make numbers of digits after decimal to be printed dynamically(i.e according to the input sometimes 16 digits sometimes 15 digits after decimal) or is their any other way tweak the number of digits after decimal.

Thanks
 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I have added code tags to your post; you should always use them. See how much better it looks Never import java.lang.Math.
You have misunderstood the figures in the format String. 1. does not mean one digit then the decimal point. It means take 1 space for the whole output. You want 16 for the places after the decimal point, 1 for the decimal point and two before the decimal point making %19.16f
Add %n so you get a new line at the end of the output.
You realise that a double is only precise to about 15.9 decimal places, so you are guaranteed inaccurate output if you try to print 18 digits, as in %19.16f?
 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Never import java.lang.Math.

I had a little problem with understanding why you suggested that.
I was thinking "What is wrong with Math that it should not be imported???"
Only after a little time I realized what you meant by this. You should not import anything from java.lang package because there is an implicit import java.lang.*
 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I wasn't clear there, was I?
 
Nikhil Nair
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the reply.


I am working on this assignment.I wrote this program keeping in mind the output the console showed me.

Browser enterd 12 as input square root result: ((Desired output) 3.4641016151377544 , (My Output) 3.4641016151377544 ) , quadratic root result ( (Desired output) 1.8612097182041991 , (My Output) 1.861209718204199 ) , Square root result was ok but quadratic root result was 1 short of required result so i increased the precision to 16 digit but when i compiled it again this time browsers input was 235.3 square root result: ((Desired output) 15.339491516996253 , (My Output) 15.339491516996253 ) , quadratic root result ( (Desired output) 3.916566291663688 , (My Output) 3.9165662916636880 ) Again my result had one digit extra like 16 but the desired output had 15 digits. My question is is their any way to control the output according to input like how can i know what user input will give them 15th precision or 16th.




Hope you all understood what i am trying to say
Thanks
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!