Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    We cannot format large numbers of type Double

    I am using:
    * SmartClient Version: v8.3p_2013-04-23/PowerEdition Deployment (built 2013-04-23)
    * Firefox version 11.0

    Our application expects users to enter large decimal values; for example 21 digits before the decimal point and 9 digits after it. We need to convert these large numbers to include thousand separators; e.g. a user enters “1234567891234567890.1234567” and we convert it to “12,345,678,901,234,567,890.1234567”. We use setEditorValueFormatter and setEditorValueParser; these are replicated in the example code below. However, when we enter a value that is bigger than the maximum a Long can hold (i.e. 2^63 – 1, or 9223372036854775807), the value is set to this maximum + 1. To give you an example, we entered 12345678901234567890 (see attached screenshot fig1.png) and the formatter changed it to 9,223,372,036,854,775,808.00 (see attached screenshot fig2.png).
    Is there a fault in SGWT whereby a large number is always passed to the Formatter as a Long rather than a Double?
    Your help in this matter will be greatly appreciated.
    Many thanks.

    Code:
    package com.tracefinancial.myApp.client;
    
    import com.google.gwt.core.client.EntryPoint;
    import com.google.gwt.i18n.client.NumberFormat;
    import com.smartgwt.client.data.Record;
    import com.smartgwt.client.widgets.form.*;
    import com.smartgwt.client.widgets.form.fields.FormItem;
    import com.smartgwt.client.widgets.form.fields.TextItem;
    
    /**
     * TODO
     * @author INSERT USERNAME INTO TEMPLATE HERE
     */
    public class GPTest implements EntryPoint {
    
      public void onModuleLoad() {
    
        DynamicForm frm = new DynamicForm();
    
        final NumberFormat fmt = NumberFormat.getFormat("#,##0.00");
    
        TextItem money = new TextItem("MyMoney");
        money.setWidth(200);
    
        money.setEditorValueFormatter(new FormItemValueFormatter() {
          @Override
          public String formatValue(Object value, Record record, DynamicForm dynamicForm,
                                    FormItem formItem) {
            if (value == null) {
              return null;
            } else {
              try {
                if (value instanceof Integer) {
                  return fmt.format((Integer)value);
                } else {
                  return fmt.format(Double.valueOf(value.toString()));
                }
              }
              catch (NumberFormatException ignore) {
                return value.toString();
              }
            }
          }
        });
    
        money.setEditorValueParser(new FormItemValueParser() {
          @Override
          public Object parseValue(String value, DynamicForm form, FormItem item) {
            if (value == null || value.isEmpty()) {
              return null;
            } else {
              try {
                return fmt.parse(value);
              }
              catch (NumberFormatException ignore) {
                return value;
              }
            }
          }
        });
    
        TextItem nextField = new TextItem("nextField");
        nextField.setWidth(200);
        frm.setFields(money, nextField);
        frm.show();
      }
    }
    Attached Files

    #2
    You're hitting the native representation limits of JavaScript's Number type.

    The best workaround is to internally store the number as a String, which works fine so long as you don't need to do math operations on these values inside the browser (which would be a bad idea anyway).

    Comment


      #3
      Thanks.
      I changed the parser and formatter to work with a string and it works.

      Comment

      Working...
      X