Java

Moderators: zibadian
Number of threads: 7818
Number of posts: 18218

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Program rounds up figures, how can i block that? Posted by GyronMkwebo on 30 Apr 2007 at 1:35 AM
I have a function that retrieves the tarriff rate for a region, given the tarriff id and the region id. The function looks as follows:
public double getTarriffDutyRate(int tarriffId,int regionId){
		 double retVal=0.00;
		 try{
			 statement=connection.createStatement();
			 ResultSet rs=statement.executeQuery("SELECT duty_rate FROM Tarriff_region_matrix WHERE " +
			 		"tarriff_id=" + tarriffId + " AND region_id=" + regionId);
			 if(rs.next()){
				 retVal=rs.getDouble("duty_rate");
			 }
		 }catch(SQLException expSQL){
			 System.out.println("Error executing: ");
			 expSQL.printStackTrace();
		 }catch(Exception exp){
			 System.out.println("Error executing: " );
			 exp.printStackTrace();
		 } 
		 return retVal;
	 }


The problem is now that when i display the data in a JTable cell, i find that the figures are rounded up, e.g 17.5% is shown as 18%. The cells are rendered by this class:
public class PercentRenderer extends DefaultTableCellRenderer {

	public PercentRenderer() {
		super();
		setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
	}

	public void setValue(Object value) {
		if ((value != null) && (value instanceof Number)) {
			Number numberValue = (Number) value;
			NumberFormat formatter = NumberFormat.getPercentInstance();
			value = formatter.format(numberValue.doubleValue());
		} 
		super.setValue(value);
	} 

} 


Can anyone help me block the rounding up of data? Is there something wrong with my renderer, coz in the table, the data is correctly saved as 0.175? Also how should a structure the Editor for the cells so that it shows the % sign when editing?
Report
Re: Program rounds up figures, how can i block that? Posted by zibadian on 30 Apr 2007 at 2:19 AM
: I have a function that retrieves the tarriff rate for a region,
: given the tarriff id and the region id. The function looks as
: follows:
:
: 
: public double getTarriffDutyRate(int tarriffId,int regionId){
: 		 double retVal=0.00;
: 		 try{
: 			 statement=connection.createStatement();
: 			 ResultSet rs=statement.executeQuery("SELECT duty_rate FROM Tarriff_region_matrix WHERE " +
: 			 		"tarriff_id=" + tarriffId + " AND region_id=" + regionId);
: 			 if(rs.next()){
: 				 retVal=rs.getDouble("duty_rate");
: 			 }
: 		 }catch(SQLException expSQL){
: 			 System.out.println("Error executing: ");
: 			 expSQL.printStackTrace();
: 		 }catch(Exception exp){
: 			 System.out.println("Error executing: " );
: 			 exp.printStackTrace();
: 		 } 
: 		 return retVal;
: 	 }
: 
:
:
: The problem is now that when i display the data in a JTable cell, i
: find that the figures are rounded up, e.g 17.5% is shown as 18%. The
: cells are rendered by this class:
:
: 
: public class PercentRenderer extends DefaultTableCellRenderer {
: 
: 	public PercentRenderer() {
: 		super();
: 		setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
: 	}
: 
: 	public void setValue(Object value) {
: 		if ((value != null) && (value instanceof Number)) {
: 			Number numberValue = (Number) value;
: 			NumberFormat formatter = NumberFormat.getPercentInstance();
: 			value = formatter.format(numberValue.doubleValue());
: 		} 
: 		super.setValue(value);
: 	} 
: 
: } 
: 
:
:
: Can anyone help me block the rounding up of data? Is there something
: wrong with my renderer, coz in the table, the data is correctly
: saved as 0.175? Also how should a structure the Editor for the cells
: so that it shows the % sign when editing?

Appearantly the locale of your computer is set to whole numbered percentages only. You could change the percentage format for your locale, or you can use another formatter. Another option is to cast the number into a double and use the Double class to change it into a String. In code:
  value = Double(Number.doubleValue()).toString+"%";

this should not round the value.
Report
Re: Program rounds up figures, how can i block that? Posted by GyronMkwebo on 30 Apr 2007 at 3:56 AM
I tried to change the locale using this: -
NumberFormat formatter = NumberFormat.getPercentInstance(Locale.US);


but it didn't make a difference.
How would i change/specify the locale in code to make it recognise decimal percentages?

And also, after editing a column, the figures in the table lose their proper formatting (this is because i would have refreshed the table with fireTableChanged() method).
I do not want this to happen, instead, i want the percent signs to be preserved.
I have invoked the renderer thus:

TableColumnModel tmTf=tfTable.getColumnModel();
		for(int i=1;i<tfModel.getColumnCount();i++){
			TableColumn tcTf=tmTf.getColumn(i);
			tcTf.setCellRenderer(new PercentRenderer());
		}


This works before the table refreshes. May you help me preserve the formatt?
Apparently, i do not understand as much about cell editors as i thought
Report
Re: Program rounds up figures, how can i block that? Posted by zibadian on 30 Apr 2007 at 5:19 AM
: I tried to change the locale using this: -
:
: 
: NumberFormat formatter = NumberFormat.getPercentInstance(Locale.US);
: 
:
:
: but it didn't make a difference.
: How would i change/specify the locale in code to make it recognise
: decimal percentages?
:
: And also, after editing a column, the figures in the table lose
: their proper formatting (this is because i would have refreshed the
: table with fireTableChanged() method).
: I do not want this to happen, instead, i want the percent signs to
: be preserved.
: I have invoked the renderer thus:
:
:
: 
: TableColumnModel tmTf=tfTable.getColumnModel();
: 		for(int i=1;i<tfModel.getColumnCount();i++){
: 			TableColumn tcTf=tmTf.getColumn(i);
: 			tcTf.setCellRenderer(new PercentRenderer());
: 		}
: 
:
:
: This works before the table refreshes. May you help me preserve the
: formatt?
: Apparently, i do not understand as much about cell editors as i
: thought
Changing the Locale must be done outside the program. In Windows it is one of the Configuration screen options.

You can always do this:
	public void setValue(Object value) {
		if ((value != null) && (value instanceof Number)) {
			value = Double(Number.doubleValue()).toString+"%";
		} 
		super.setValue(value);
	} 

This should preserve the rounding. The only drwback is that you don't use the computer's Locale.
Report
Re: Program rounds up figures, how can i block that? Posted by GyronMkwebo on 30 Apr 2007 at 6:09 AM
I am using AbstractTableModel so super.setValue doesn't work.
How do i call/invoke the computer's locale in code?

Report
Re: Program rounds up figures, how can i block that? Posted by zibadian on 30 Apr 2007 at 6:37 AM
: I am using AbstractTableModel so super.setValue doesn't work.
: How do i call/invoke the computer's locale in code?
:
:
This code was based on your original post, where your use a subclass of the DefaultTableCellRenderer. In that code you also used super.setValue(), so why wouldn't it work now?

To gain access to the locale, check out the Locale class: http://java.sun.com/j2se/1.5.0/docs/api/java/util/Locale.html
Report
Re: Program rounds up figures, how can i block that? Posted by GyronMkwebo on 30 Apr 2007 at 7:02 AM
Oh, stupid me, i was confusing setValue() with setValueAt(), sorry my mistake.
What do you suppose is the best link for cellEditors? It looks like i need to have a look at that stuff too.



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.