?
Solved

How do I change the number format?

Posted on 2005-05-01
7
Medium Priority
?
288 Views
Last Modified: 2010-03-31
Hi,

I am using Java to interrogate an Access database.  One of the fields is Price.  I need price to be displayed in a currency format.  At the moment I have it working to display eg "£" 6.0, but I really need to display "£" 6.00.

float price = rs.getFloat("Price");

I have tried using number formatt..which said it wouldnt work with a float..so i changed it to a string....now there are no errors, but when you run it, it doesnt work.

String price = rs.getString("Price");
                price = NumberFormat.getCurrencyInstance().format(price);

If i can get this to work, will this only put it into Dollars???

Thanks

0
Comment
Question by:Sophie27
  • 5
  • 2
7 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 13904678
NumberFormat  nf = NumberFormat.getCurrencyInstance(LOCALE.UK);
nf.setMinimumFractionDigits(2);
nf.setMaximumFractionDigits(2);
String sPrice = nf.format(price);
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13904682
Sorry, that should have been

NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.UK);
0
 

Author Comment

by:Sophie27
ID: 13904697
It is complaining about the Locale bit...which ever way i type it in.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 86

Expert Comment

by:CEHJ
ID: 13904716
You need to import it or do

NumberFormat nf = NumberFormat.getCurrencyInstance(java.util.Locale.UK);
0
 

Author Comment

by:Sophie27
ID: 13904755
The compilor says that there are no errors, but when i run the program, price still comes up as  6.0  I was hoping to get £ 6.00

Any ideas?

import java.sql.*;
import java.text.NumberFormat;
import java.io.*;

public class queryCost
{
    static BufferedReader kb = new BufferedReader(new InputStreamReader(System.in));
   
    public static void main (String[] args)
    {
        String url = "jdbc:odbc:Plants";
        Connection con;
       
        String queryCost ="SELECT* from Plants WHERE Name = ?";
        System.out.print("Please enter the name of the plant about which you wish to enquire: ");
        String cost = getInput();
       
        try
        {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        }
        catch (ClassNotFoundException e)
        {
            System.err.print("ClassNotFoundException: ");
            System.err.println(e.getMessage());
        }
        try
        {
            con=DriverManager.getConnection(url);
            PreparedStatement search = con.prepareStatement(queryCost);
            search.setString(1, cost);
            ResultSet rs = search.executeQuery();
            while(rs.next())
            {
                String name = rs.getString("Name");
                String latin = rs.getString("Latin");
                String stock = rs.getString("In Stock");
                float price = rs.getFloat("Price");
                NumberFormat  nf = NumberFormat.getCurrencyInstance(java.util.Locale.UK);
                nf.setMinimumFractionDigits(2);
                nf.setMaximumFractionDigits(2);
                String sPrice = nf.format(price);
                System.out.println("\n"+name + "("+latin+")"+ "= £" + price + "\n Number in Stock "+stock+"\n");
            }
            search.close();
            con.close();
        }
        catch(Exception e)
        {
            System.err.println("SQLException: "+e.getMessage());
        }
    }
    public static String getInput()
    {
        String value = null;
        try
        {
            value=kb.readLine();
        }
        catch(IOException e)
        {
            System.err.println("Error in input");
        }
        return value;
    }
}
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 1000 total points
ID: 13904770
>> System.out.println("\n"+name + "("+latin+")"+ "= £" + price + "\n Number in Stock "+stock+"\n");

You're using 'price' instead of 'sPrice'. Also that NumberFormat should be constructed before the loop begins
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13904914
:-)
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month16 days, 15 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question