The Artima Developer Community
Sponsored Link

Java Answers Forum
Formatting number length

2 replies on 1 page. Most recent reply: Jul 25, 2002 4:05 AM by thomas

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Jenga

Posts: 3
Nickname: topazz
Registered: Jul, 2002

Formatting number length Posted: Jul 24, 2002 4:20 AM
Reply to this message Reply
Advertisement
I have a quick question. I have developed a program to find square roots and display the results in a table. I would like the square root to be only 4 decimal places, example 5.8792 not 5.879234521. How can i control the number of decimal places?


thomas

Posts: 42
Nickname: turbomanic
Registered: Jul, 2002

Re: Formatting number length Posted: Jul 24, 2002 8:45 AM
Reply to this message Reply
use the DecimalFormat class. We use that in university so I say it is available in all java libaries.

write this:
DecimalFormat largenumber= new DecimalFormat("0.####");
Final double start=5.879234521;
double answer= largenumber.format(start);
//answer should now only have 4 decimal places
//like you wanted

thomas

Posts: 42
Nickname: turbomanic
Registered: Jul, 2002

Re: Formatting number length Posted: Jul 25, 2002 4:05 AM
Reply to this message Reply
I forgot to mention you must import java.text.* to use decimalformat class.

Flat View: This topic has 2 replies on 1 page
Topic: how to run applets in browsers Previous Topic   Next Topic Topic: how to strip html tags using java

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use