Announcement

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

    Number formatting options

    I'm working my way through the various i18n issues and have found what seems like the perfect solution for dates using GWTs i18n methodology. I put the following code at the start of my module and it seems to do exactly what I want, but please let me know if this is not the best approach. Locale is specified on the URL.
    Code:
    DateUtil.setDateInputFormatter(new DateInputFormatter() {
    	@Override
    	public Date parse(String dateString) {
    		return DateTimeFormat.getShortDateFormat().parse(dateString);
    	}
    });
    DateUtil.setShortDateDisplayFormatter(new DateDisplayFormatter() {
    	@Override
    	public String format(Date date) {
    		return DateTimeFormat.getShortDateFormat().format(date);
    	}
    });
    DateUtil.setNormalDateDisplayFormatter(new DateDisplayFormatter() {
    	@Override
    	public String format(Date date) {
    		return DateTimeFormat.getMediumDateFormat().format(date);
    	}
    });
    The next thing I need to tackle is number formatting. I'd like to be able to specify a "money" data type that will format 12345.67 as either $12,345.67 or £12,345.67 or € 12.345,67 depending on the locale. And for integers or other non-currency fields I'd like to do the same so 12345 becomes 12,345 or 12.345 and 12345.6789 becomes 12,345.6789 or 12.345,6789 depending on the locale.

    What is the recommended approach for this?

    #2
    The best thing is to create a SimpleType and add formatters that conditionally use different separator chars based on the locale (however you set/detect it).

    Comment


      #3
      I've come back to try to solve this issue with a SimpleType but can't seem to get it. I'm setting the locale via a parameter on the URL and have successfully formatted dates and translated text strings - just can't figure out how number formatting is supposed to work. I've read up on GWT NumberFormat class and can use it successfully but it isn't apparent to me how create a SmartGWT SimpleType subclass that handles formatting. I see methods to setInheritsFrom(FieldType.FLOAT) and setValidators but nothing regarding how to implement formatting. The only example I can find only shows how validation works (Zip Code example).

      Could you please provide an example of a SimpleType that formats currency values based on the current locale?

      Comment


        #4
        I'll second that

        I'm interested in a currency field, but have yet to find a solution. I don't see anything for SimpleType except for setting validators. It appears that the GWT version is missing a few methods.. specifically "normalDisplayFormatter" seems like a candidate a good candidate with no matching method on the GWT side.

        Comment


          #5
          Isomorphic?

          Is there a way to implement number formatters for a SimpleType in SmartGWT?

          Comment


            #6
            At the moment the SGWT APIs do not support centralized formatter definition, however, it can be done via JSNI, or, to get a similar effect to the SimpleType system, you can create subclasses of various components that recognize custom field types and automatically add formatters.

            Comment


              #7
              Could you please post a simple example? It isn't clear to me how I would mix the use of GWT's NumberFormat class with the rest in JSNI.

              Comment


                #8
                Any help? This must be relatively easy for someone who is adept at JSNI, but I can't seem to figure it out. It also seems like a very common requirement for anyone using SmartGWT. There are a number of forum threads dealing with the issue yet I can't find a single one with a satisfactory solution using gwt.i18n.client.NumberFormat and I don't want to code something in javascript that will only handle a few cases. Date formatting is so simple, why not number formatting?

                Comment


                  #9
                  Hi Jay,

                  We haven't been creating a JSNI sample because we're going to add SGWT APIs so you don't need JSNI. If you need something this week for a demo, just add formatters to the specific widgets where you need to show formatted numbers.

                  Comment


                    #10
                    If the SGWT APIs are coming soon(ish) I can wait.

                    Thanks!

                    Comment


                      #11
                      I see the displayFormatter overrides were added with SGWT 2.1. Thanks!

                      I've coded the following and specified type="currency" on a field in my data source, but showing that field in a ListGrid and detail viewer just shows the unformatted number as a string. What am I missing?
                      Code:
                      // Create a SimpleType for currency fields and set the formatters.
                      NumberFormat currencyFormat = NumberFormat.getCurrencyFormat("USD");
                      SimpleType currencyType = new SimpleType("currency", FieldType.FLOAT);
                      currencyType.setNormalDisplayFormatter(new SimpleTypeFormatter() {
                      
                      	@Override
                      	public String format(Object value, DataClass field,
                      			DataBoundComponent component, Record record) {
                      		return currencyFormat.format(Double.valueOf(value.toString()));
                      	}
                      });
                      currencyType.setShortDisplayFormatter(new SimpleTypeFormatter() {
                      
                      	@Override
                      	public String format(Object value, DataClass field,
                      			DataBoundComponent component, Record record) {
                      		return currencyFormat.format(Double.valueOf(value.toString()));
                      	}
                      });

                      Comment


                        #12
                        If you're using a serverside datasource and specifying this SimpleType then you need to explicitly call currencyType.register()

                        Sanjiv

                        Comment


                          #13
                          That did it.

                          Thanks!

                          Comment

                          Working...
                          X