Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rating: Thread Rating: 3 votes, 3.00 average.
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-30-2004, 09:51 PM   PM User | #1
ragol_67
Regular Coder

 
Join Date: Sep 2002
Location: Calgary, AB
Posts: 179
Thanks: 0
Thanked 0 Times in 0 Posts
ragol_67 is an unknown quantity at this point
Decimals in Java

How do you assign a certain number of decimal points in a java program?

When I print out a message using System.out.println, the system prints out numbers with about 10 decimal places.

Thanks,
Nick!
ragol_67 is offline   Reply With Quote
Old 09-30-2004, 10:01 PM   PM User | #2
Jason
Regular Coder

 
Join Date: Feb 2003
Location: California
Posts: 925
Thanks: 0
Thanked 0 Times in 0 Posts
Jason is an unknown quantity at this point
Code:
import java.text.NumberFormat;
..................
      NumberFormat nf = NumberFormat.getNumberInstance() ;
      nf.setGroupingUsed(false) ;     // don't group by threes
      nf.setMaximumFractionDigits(2) ;
      nf.setMinimumFractionDigits(2) ;
      System.out.println("float unformatted " + d1 + " now formatted " + nf.format(d1));


Jason
Jason is offline   Reply With Quote
Old 10-01-2004, 07:34 AM   PM User | #3
Celtboy
Regular Coder

 
Join Date: May 2002
Location: Virginia, USA
Posts: 620
Thanks: 0
Thanked 6 Times in 6 Posts
Celtboy is an unknown quantity at this point
it also depends on what type your variables are. Floats, doubles, ints...

Otherwise the numberformat class is the way to go.
Celtboy is offline   Reply With Quote
Old 11-16-2004, 11:45 PM   PM User | #4
ragol_67
Regular Coder

 
Join Date: Sep 2002
Location: Calgary, AB
Posts: 179
Thanks: 0
Thanked 0 Times in 0 Posts
ragol_67 is an unknown quantity at this point
My variables are declared as doubles.

Could someone please give me an example of how to implement the above code?


Thanks,
Nick!
ragol_67 is offline   Reply With Quote
Old 11-17-2004, 12:41 AM   PM User | #5
Brandoe85
teh Moderatorinator


 
Join Date: Sep 2004
Location: USA
Posts: 2,472
Thanks: 4
Thanked 40 Times in 40 Posts
Brandoe85 will become famous soon enough
That code is all you need:
PHP Code:
import java.text.NumberFormat;
public class 
decimals

{
    public static 
void main(String[] args)

    {
        
NumberFormat nf NumberFormat.getNumberInstance() ;
        
nf.setGroupingUsed(false) ;     // don't group by threes
        
nf.setMaximumFractionDigits(2) ;
        
nf.setMinimumFractionDigits(2) ;
        
double d1 3.427223;
          
System.out.println("float unformatted " d1 " now formatted " nf.format(d1));

    }


__________________
-Brando
Why using tables for eating is stupid!
Brandoe85 is offline   Reply With Quote
Old 11-17-2004, 01:39 AM   PM User | #6
ragol_67
Regular Coder

 
Join Date: Sep 2002
Location: Calgary, AB
Posts: 179
Thanks: 0
Thanked 0 Times in 0 Posts
ragol_67 is an unknown quantity at this point
Ah, okay! I've got it.

Thank You!!

ragol_67 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting 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

Forum Jump


All times are GMT +1. The time now is 03:41 PM.


Advertisement
Log in to turn off these ads.