: 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.