Problems with comma(,) seperator.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ajos
    Contributor
    • Aug 2007
    • 283

    Problems with comma(,) seperator.

    I have to apply a format to my currency value.

    The currency i have is-->
    1402000
    15000
    10000 and so on.

    I want to display it as--->
    14,02,000
    15,000
    10,000

    What i've done is,

    [CODE=java]
    String value = mfobj.getTotalC ost();//getting the value
    int i = Integer.valueOf (value).intValu e();//converting to int
    Locale india = new Locale("en","IN ");//custom locale
    NumberFormat nf = NumberFormat.ge tInstance(india );
    DecimalFormat df = (DecimalFormat) nf;//instantiate a NumFormat ,cast it to DecimalFormat
    df.applyPattern ("##,##,###" );//pattern applying
    //String output = df.format(i);
    //Currency curr = Currency.getIns tance(india);

    display it as <%=df.format( i) %>
    [/CODE]

    the values im getting is
    1,402,000
    15,000
    10,000

    How can i achieve the above combination?

    Regards,

    ajos
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Straight from the API documentation:

    Originally posted by API
    The grouping separator is commonly used for thousands, but in some countries it separates ten-thousands. The grouping size is a constant number of digits between the grouping characters, such as 3 for 100,000,000 or 4 for 1,0000,0000. If you supply a pattern with multiple grouping characters, the interval between the last one and the end of the integer is the one that is used. So "#,##,###,# ###" == "######,### #" == "##,####,## ##".
    I find the last sentence a bit of a deficiency because it implies that what you want
    can't be done.

    kind regards,

    Jos

    Comment

    • ajos
      Contributor
      • Aug 2007
      • 283

      #3
      Originally posted by JosAH
      Straight from the API documentation:



      I find the last sentence a bit of a deficiency because it implies that what you want
      can't be done.

      kind regards,

      Jos
      Thanks jos, yes i forgot to mention that ive read the docs and didnt find any way. If there is any way around for this, kindly advice.

      Regards,

      ajos.

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by ajos
        Thanks jos, yes i forgot to mention that ive read the docs and didnt find any way. If there is any way around for this, kindly advice.

        Regards,

        ajos.
        I googled a bit but it seems that it can't be done without hacking; the only
        reference I found was about this class: com.ibm.icu.tex t.NumberFormat but
        I don't think you want to go that route. The standard NumberFormat or
        DecimalFormat classes simply can't do what you want (which I find a bit
        peculiar). Sorry, no cookbook solution from me.

        kind regards,

        Jos

        Comment

        • ajos
          Contributor
          • Aug 2007
          • 283

          #5
          Originally posted by JosAH
          I googled a bit but it seems that it can't be done without hacking; the only
          reference I found was about this class: com.ibm.icu.tex t.NumberFormat but
          I don't think you want to go that route. The standard NumberFormat or
          DecimalFormat classes simply can't do what you want (which I find a bit
          peculiar). Sorry, no cookbook solution from me.

          kind regards,

          Jos
          Thanks jos for your time and effort :).
          Its a pity that we dont have enough support for custom locales(interna tional formats) and more power in the pattern we want.

          I guess i have to be (hesitantly)hap py with what ive done for now.

          Thanks and regards,

          ajos :)

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by ajos
            Thanks jos for your time and effort :).
            Its a pity that we dont have enough support for custom locales(interna tional formats) and more power in the pattern we want.

            I guess i have to be (hesitantly)hap py with what ive done for now.

            Thanks and regards,

            ajos :)
            What I find particularly silly is that pseudo-intelligently deciding that the grouping
            size can be deduced from the rightmost grouping character in the format string.
            Maybe Sun's bugbase know more about it ... I personally find it a design flaw,
            not a feature. Suppose I want to do this: "#,##,###,####, ##". I want it and I don't
            want any stupic class that determines that the group size equals 2 ...

            Of course a hack around it can be written but what do we have those format
            classes for then? Oh well, Sun has done more stupider things ...

            kind regards,

            Jos

            Comment

            • ajos
              Contributor
              • Aug 2007
              • 283

              #7
              Originally posted by JosAH
              ......
              Maybe Sun's bugbase know more about it ... I personally find it a design flaw,
              not a feature. Suppose I want to do this: "#,##,###,####, ##". I want it and I don't
              want any stupic class that determines that the group size equals 2 ...

              Of course a hack around it can be written but what do we have those format
              classes for then? Oh well, Sun has done more stupider things ...

              kind regards,

              Jos
              I know this is an old thread, just wanted to point out a bug report which i found today to save others from further searching.Bug

              Hope That Helps,

              ajos

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #8
                Originally posted by ajos
                I know this is an old thread, just wanted to point out a bug report which i found today to save others from further searching.Bug

                Hope That Helps,

                ajos
                Well, it only helps to show how narrow minded assumptions are. I still find it a
                bit peculiar that the position of all but the rightmost comma are ignored. Why
                put them in a format specification then? I read that this deficiency has a low
                priority on Sun's list so I don't expect a fix anytime soon ...

                kind regards,

                Jos

                Comment

                • blazedaces
                  Contributor
                  • May 2007
                  • 284

                  #9
                  Originally posted by JosAH
                  Well, it only helps to show how narrow minded assumptions are. I still find it a
                  bit peculiar that the position of all but the rightmost comma are ignored. Why
                  put them in a format specification then? I read that this deficiency has a low
                  priority on Sun's list so I don't expect a fix anytime soon ...

                  kind regards,

                  Jos
                  Is there a way to make it easier by doing something along the lines of extending DecimalFormat and overriding that specific set pattern method?

                  I can't tell if what I'm talking about is impossible...

                  -blazed

                  Comment

                  • JosAH
                    Recognized Expert MVP
                    • Mar 2007
                    • 11453

                    #10
                    Originally posted by blazedaces
                    I can't tell if what I'm talking about is impossible...
                    It's not impossible of course but it'd take almost a rewrite of that class. Have
                    a look at its source (in file src.zip in your jdk directory) and see for yourself.

                    kind regards,

                    Jos

                    Comment

                    • ajos
                      Contributor
                      • Aug 2007
                      • 283

                      #11
                      Originally posted by JosAH
                      It's not impossible of course but it'd take almost a rewrite of that class. Have
                      a look at its source (in file src.zip in your jdk directory) and see for yourself.

                      kind regards,

                      Jos
                      Yes its a long standing issue which i think needs to be fixed(or may be rewritten?) considering Java is used widely all around. There is also a related bug if anybody noticed Here.

                      regards,

                      ajos

                      Comment

                      • chaarmann
                        Recognized Expert Contributor
                        • Nov 2007
                        • 785

                        #12
                        Can't you do something with regular expressions to get it formatted?
                        Escpecially if you have it as String already?
                        I haven't verified the code below, but I hope you get the idea.

                        Code:
                        String s = "1402000" // your number to be formatted
                        Pattern p = Pattern.compile("(.{2})(.{2})(.{3})$");
                        Matcher m = p.matcher(s);
                        String formattedString = String.format("%s,%s,%s"), m.group(1), m.group(2), m.group(2));
                        Just make the regular expression pattern for the biggest number expected, in my case 7. If you have number smaller than 7 characters, for example "1234", fill the beginning with spaces and trim away leading spaces and commas afterwards:

                        Code:
                        String s = "       " +"1234"; // append 7 spaces in front, no matter how big the number is.
                        ...
                        formattedString=  formattedString.replace("^[ ,]+", "");
                        or you could try with
                        Code:
                        s.split("(^.{1,2}.{2}.{3}$)|(^.{1,2}.{3}$)|(^.{1,3}$)")
                        and append the pieces somehow. I think there is a nice functions in jakarta commons library to do it with a single command.

                        Comment

                        Working...