Announcement

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

    Number conversion problem from EditorValueParser to EditorValueFormatter

    Hello,

    I am using SmartGWT 3.0 (but the developer console shows SmartClient Version: 8.2/LGPL Development Only (built 2011-12-05)), GWT 2.4 and Firefox 11.0

    I have a TextItem with an EditorValueParser and EditorValueFormatter. The TextItem receives a long value (a timestamp) and it should represent it as a double value (hours). It is working very well when the long value comes directly from the server and value is represented as it should be in hours. But when the user types in the TextItem the value 999 after validation the value that appears in the TextItem is 999.0000355555555. This is just an example how to reproduce the problem.

    In fact the problem is that the value returned by the parser (e.g long 144000000) is not the same as the one received by the formater (e.g long 1440000128) and i can not figure out why the underlying value is altered .

    Here is a sample that reproduces the problem. Keep in mind that only for certain values is reproduced (e.g. 999, 10000)

    Code:
    public class Test extends Window {
    		private TextItem workItem;
    		private DynamicForm updateworkForm;
    
    		public Test() {
    			super();
    			workItem = new TextItem();
    			workItem.setTitle("Work");
    			workItem.setEditorValueFormatter(new FormItemValueFormatter() {
    				@Override
    				public String formatValue(Object value, Record record,
    						DynamicForm form, FormItem item) {
    					if (value != null) {
    						if (value instanceof Number) {
    							long work = ((Number) value).longValue();
    							return (work / (1000d * 60 * 60)) + "";
    						} else {
    							return null;
    						}
    					} else {
    						return null;
    					}
    				}
    			});
    			workItem.setEditorValueParser(new FormItemValueParser() {
    				@Override
    				public Object parseValue(String value, DynamicForm form,
    						FormItem item) {
    					if (value != null) {
    						String integerPartString = "";
    						String fractionaryPartString = "";
    						int pointPos = value.indexOf(".");
    						if (pointPos < 0) {
    							integerPartString = value.substring(0,
    									value.length());
    						} else if (pointPos == 0) {
    							return null;
    						} else {
    							integerPartString = value.substring(0, pointPos);
    							if (value.indexOf(".", pointPos + 1) == -1) {
    								fractionaryPartString = value.substring(
    										pointPos + 1, value.length());
    							} else {
    								return null;
    							}
    						}
    
    						long integerPart = 0;
    						long fractionaryPart = 0;
    						if (!integerPartString.equals("")) {
    							integerPart = Long.parseLong(integerPartString) * 1000 * 60 * 60;
    						}
    						if (!fractionaryPartString.equals("")) {
    							fractionaryPart = (long) (Double
    									.parseDouble(fractionaryPartString)
    									/ Math.pow(10, value.length() - pointPos
    											- 1) * 1000 * 60 * 60);
    						}
    
    						long number = integerPart + fractionaryPart;
    						if (number <= Integer.MAX_VALUE) {
    							return new Integer(number + "");
    						} else {
    							return new Long(number);
    						}
    					}
    					return null;
    				}
    			});
    
    			updateworkForm = new DynamicForm();
    			updateworkForm.setFields(workItem);
    
    			this.addItem(updateworkForm);
    			this.setWidth(500);
    			this.setHeight(500);
    		}
    	}

    #2
    Does this happen in the Development mode only?
    Have you tried it in compiled mode?
    MichalG

    Comment


      #3
      Originally posted by michalg View Post
      Does this happen in the Development mode only?
      Have you tried it in compiled mode?
      MichalG
      Thank you for the hint. It looks like that in compiled mode everything works fine. So what is the problem and how can i fix it in order to also work in development mode?

      Comment


        #4
        Well, I am not gwt guru at all. Just happened to have similar problem and my conclusion was that it hits me in the Development only. I can live with this.

        You may follow some discussions here:
        ""float" field is actually JavaScript's Number type: more than Java's float, less than Java's double (simplifying here - there are nuances)."
        http://forums.smartclient.com/showthread.php?t=17758&highlight=number+float
        HTH
        MichalG

        Comment


          #5
          Originally posted by michalg View Post
          Well, I am not gwt guru at all. Just happened to have similar problem and my conclusion was that it hits me in the Development only. I can live with this.

          You may follow some discussions here:
          ""float" field is actually JavaScript's Number type: more than Java's float, less than Java's double (simplifying here - there are nuances)."
          http://forums.smartclient.com/showthread.php?t=17758&highlight=number+float
          HTH
          MichalG
          Thank you Michal for your help. For the moment i can live with this but I am also waiting for a response from Isomorphic.

          Comment

          Working...
          X