Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Program rounds up figures, how can i block that?

GyronMkweboGyronMkwebo Posts: 224Member ✭✭
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:
[code]
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;
}
[/code]

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:
[code]
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);
}

}
[/code]

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?

Comments

  • zibadianzibadian Posts: 6,349Member
    : 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:
    : [code]:
    : 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;
    : }
    : [/code]:
    :
    : 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:
    : [code]:
    : 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);
    : }
    :
    : }
    : [/code]:
    :
    : 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:
    [code]
    value = Double(Number.doubleValue()).toString+"%";
    [/code]
    this should not round the value.
  • GyronMkweboGyronMkwebo Posts: 224Member ✭✭
    I tried to change the locale using this: -
    [code]
    NumberFormat formatter = NumberFormat.getPercentInstance(Locale.US);
    [/code]

    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:

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

    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 ;-)
  • zibadianzibadian Posts: 6,349Member
    : I tried to change the locale using this: -
    : [code]:
    : NumberFormat formatter = NumberFormat.getPercentInstance(Locale.US);
    : [/code]:
    :
    : 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:
    :
    : [code]:
    : TableColumnModel tmTf=tfTable.getColumnModel();
    : for(int i=1;i<tfModel.getColumnCount();i++){
    : TableColumn tcTf=tmTf.getColumn(i);
    : tcTf.setCellRenderer(new PercentRenderer());
    : }
    : [/code]:
    :
    : 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:
    [code]
    public void setValue(Object value) {
    if ((value != null) && (value instanceof Number)) {
    value = Double(Number.doubleValue()).toString+"%";
    }
    super.setValue(value);
    }
    [/code]
    This should preserve the rounding. The only drwback is that you don't use the computer's Locale.
  • GyronMkweboGyronMkwebo Posts: 224Member ✭✭
    I am using AbstractTableModel so super.setValue doesn't work.
    How do i call/invoke the computer's locale in code?

  • zibadianzibadian Posts: 6,349Member
    : 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
  • GyronMkweboGyronMkwebo Posts: 224Member ✭✭
    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.
Sign In or Register to comment.