Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > New to Java

New to Java
Thread Tools
Jaey
Mac Elite
Join Date: Dec 2003
Status: Offline
Reply With Quote
Feb 26, 2004, 01:02 AM
 
I decided to learn Java about half an hour ago, so I went through the "Developing Cocoa Java" tutorial on the Apple developer site. It pretty much makes a simple application that converts between farenheit and celcius. But I was noticing that they used int, and not float. I wanted more precise results so I changed the ints to floats. Then when I ran it, I got crazy weird numbers like 4.00000000095. So my question is, is there anyway to make it round off a float to the nearest, say, tenth?
     
JNI
Forum Regular
Join Date: Oct 2002
Location: Left Coast
Status: Offline
Reply With Quote
Feb 26, 2004, 05:54 AM
 
Originally posted by Jaey:
I decided to learn Java about half an hour ago, so I went through the "Developing Cocoa Java" tutorial on the Apple developer site. It pretty much makes a simple application that converts between farenheit and celcius. But I was noticing that they used int, and not float. I wanted more precise results so I changed the ints to floats. Then when I ran it, I got crazy weird numbers like 4.00000000095. So my question is, is there anyway to make it round off a float to the nearest, say, tenth?
Check out the NumberFormat class. You can define the min/max number of characters after the decimal point to display. It's a bit tricky of a class to use, if you still have problems, post again.
     
Jaey  (op)
Mac Elite
Join Date: Dec 2003
Status: Offline
Reply With Quote
Feb 26, 2004, 04:40 PM
 
Originally posted by JNI:
Check out the NumberFormat class. You can define the min/max number of characters after the decimal point to display. It's a bit tricky of a class to use, if you still have problems, post again.
Um yeah, I read about the NumberFormat class, but I'm still not sure how it's done...
     
Kristoff
Mac Elite
Join Date: Sep 2000
Location: in front of the keyboard
Status: Offline
Reply With Quote
Feb 26, 2004, 05:02 PM
 
There's these things called API documents. They're really cool.
signatures are a waste of bandwidth
especially ones with political tripe in them.
     
Jaey  (op)
Mac Elite
Join Date: Dec 2003
Status: Offline
Reply With Quote
Feb 26, 2004, 06:42 PM
 
Yes, but the API documents are nearly incomprehensible to my pathetic brain.
     
Chuckit
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
Feb 26, 2004, 07:40 PM
 
Originally posted by Jaey:
Yes, but the API documents are nearly incomprehensible to my pathetic brain.
Then you need to work on understanding API documents. It's not that hard, and it's pretty much essential to programming.
Code:
String formatted = NumberFormat.getNumberInstance().format(someNumber);
Pretty simple.
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
JNI
Forum Regular
Join Date: Oct 2002
Location: Left Coast
Status: Offline
Reply With Quote
Feb 27, 2004, 04:42 PM
 
Hey, come on, they guy said he was new to Java. There are hundreds of classes in the 1.4.x API, and one can't expect a newbie to know every class. let alone how to use them correctly. He asked for help, and instead got mostly slapped around. Yes, everyone should learn to read through the docs, but if someone can help point you in the right direction when there is a big set of info to scan, saving someone time, why not help out?

You create a NumberFormat class to format a string with 1 decimal place like this:

NumberFormat numberFormat = NumberFormat.getNumberInstance();
numberFormat.setMaximumFractionDigits(1);
numberFormat.setMinimumFractionDigits(1);

If you are doing the format many times, like in a loop, you can create the formatter outside of the loop and reuse it so that you don't have to construct it each time (saving lots of time.) Then when you want to format a number you do this:

String s = new String(numberFormat.format(floatValue));

a full example:

Code:
import java.text.*; public class jtool { public static void main (String args[]) { NumberFormat numberFormat = NumberFormat.getNumberInstance(); numberFormat.setMaximumFractionDigits(1); numberFormat.setMinimumFractionDigits(1); float f = 0.0f; int i = 100; while (i-- >= 0) { String s = new String(numberFormat.format(f)); System.out.println(s); f += .05f; } } }
Now that wasn't too hard to explain, was it?
     
Chuckit
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
Feb 27, 2004, 05:06 PM
 
Originally posted by JNI:
Hey, come on, they guy said he was new to Java. There are hundreds of classes in the 1.4.x API, and one can't expect a newbie to know every class. let alone how to use them correctly.
That would probably be why he was first informed about the NumberFormat class and then told to check out the API (i.e. "how to use them correctly"). He said that he couldn't understand API documentation, and I said that's a problem he needs to work on, because it is. It looks like solid advice to me, even if the pokey-stick emoticon was a little rude.
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
JNI
Forum Regular
Join Date: Oct 2002
Location: Left Coast
Status: Offline
Reply With Quote
Feb 27, 2004, 05:49 PM
 
Originally posted by Chuckit:
That would probably be why he was first informed about the NumberFormat class and then told to check out the API (i.e. "how to use them correctly"). He said that he couldn't understand API documentation, and I said that's a problem he needs to work on, because it is. It looks like solid advice to me, even if the pokey-stick emoticon was a little rude.
Yeah, I get that. But imagine if your teacher told you to read a chapter in a book, and you came back the next day and said "I read it and I didn't understand it." Then the teacher just says "well, learn how to read better." and then just walked away. Yeah, I kind of "gave him the answer", but hopefully in a way that gives an abstract usage so that from that he can maybe learn how to 'interpret' the documentation, and now he has to figure out how to apply that abstract information to his particular task. In particular, the NumberFormat class is a bit weird, using a factory type constructor and all. I can see how someone that's just starting out might not get that right off.

Whatever. Sometimes it's better to hold someone's hand a bit then to berate them. Hopefully over time things will start to click better and he can do more on his own, instead of being made to feel like an idiot and just losing self esteem.
     
Arkham_c
Mac Elite
Join Date: Dec 2001
Location: Atlanta, GA, USA
Status: Offline
Reply With Quote
Feb 27, 2004, 09:31 PM
 
I'd also suggest using Google to look for code samples. I do this all the time with great success.


I was just using it today for JAXB and JAXP samples.
Mac Pro 2x 2.66 GHz Dual core, Apple TV 160GB, two Windows XP PCs
     
suthercd
Senior User
Join Date: Oct 2000
Location: Midwest
Status: Offline
Reply With Quote
Feb 28, 2004, 04:37 PM
 
     
Jaey  (op)
Mac Elite
Join Date: Dec 2003
Status: Offline
Reply With Quote
Feb 29, 2004, 05:11 AM
 
Cool, thanks for everything.
     
TampaDeveloper
Dedicated MacNNer
Join Date: Nov 2003
Status: Offline
Reply With Quote
Mar 2, 2004, 01:52 PM
 
Originally posted by Jaey:
I decided to learn Java about half an hour ago, so I went through the "Developing Cocoa Java" tutorial on the Apple developer site. It pretty much makes a simple application that converts between farenheit and celcius. But I was noticing that they used int, and not float. I wanted more precise results so I changed the ints to floats. Then when I ran it, I got crazy weird numbers like 4.00000000095. So my question is, is there anyway to make it round off a float to the nearest, say, tenth?
Hi Jaey,

First off, don't let the flamers get you down. Just let them boost their egos by pretending to be superior. The API documents are awesome once you've used a particular class. But sometimes you just need an example to see how it all fits together.

Anyhow, another class you might look at is BigDecimal. This class is recommended for things that require high precision, such as financial calculations, etc.

Good luck on your endeavor.
     
Chuckit
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
Mar 2, 2004, 04:12 PM
 
Originally posted by TampaDeveloper:
First off, don't let the flamers get you down. Just let them boost their egos by pretending to be superior.
Oh, for crying out loud. This is the only flame in this thread. The others all contained useful advice related to the question at hand rather than personal attacks on posters.
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
TampaDeveloper
Dedicated MacNNer
Join Date: Nov 2003
Status: Offline
Reply With Quote
Mar 2, 2004, 05:00 PM
 
Originally posted by TampaDeveloper:
Anyhow, another class you might look at is BigDecimal. This class is recommended for things that require high precision, such as financial calculations, etc.
     
   
 
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 11:59 PM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,