Why was the pattern string not followed in this code?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







5















When following code is executed:



DecimalFormat eNotation1 = new DecimalFormat("#0.###E0");   
System.out.println(eNotation1.format(123.456789));


My expected output is;




1.235E2




instead,




1.2346E2




was printed.



Why was the output 1.2346E2?



I know I could have used a method like setMaximumFractionDigits() if all I wanted was to set the maximum number of digits after the decimal point, but I just really want to understand why the output was 1.2346E2










share|improve this question































    5















    When following code is executed:



    DecimalFormat eNotation1 = new DecimalFormat("#0.###E0");   
    System.out.println(eNotation1.format(123.456789));


    My expected output is;




    1.235E2




    instead,




    1.2346E2




    was printed.



    Why was the output 1.2346E2?



    I know I could have used a method like setMaximumFractionDigits() if all I wanted was to set the maximum number of digits after the decimal point, but I just really want to understand why the output was 1.2346E2










    share|improve this question



























      5












      5








      5


      1






      When following code is executed:



      DecimalFormat eNotation1 = new DecimalFormat("#0.###E0");   
      System.out.println(eNotation1.format(123.456789));


      My expected output is;




      1.235E2




      instead,




      1.2346E2




      was printed.



      Why was the output 1.2346E2?



      I know I could have used a method like setMaximumFractionDigits() if all I wanted was to set the maximum number of digits after the decimal point, but I just really want to understand why the output was 1.2346E2










      share|improve this question
















      When following code is executed:



      DecimalFormat eNotation1 = new DecimalFormat("#0.###E0");   
      System.out.println(eNotation1.format(123.456789));


      My expected output is;




      1.235E2




      instead,




      1.2346E2




      was printed.



      Why was the output 1.2346E2?



      I know I could have used a method like setMaximumFractionDigits() if all I wanted was to set the maximum number of digits after the decimal point, but I just really want to understand why the output was 1.2346E2







      java decimalformat scientific-notation






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 43 mins ago









      Strazan

      53112




      53112










      asked 1 hour ago









      Lenovo GenoviaLenovo Genovia

      485




      485
























          3 Answers
          3






          active

          oldest

          votes


















          4














          If you want that exact format, then use the pattern 0.000E0:



          DecimalFormat eNotation1 = new DecimalFormat("0.000E0");
          System.out.println(eNotation1.format(123.456789));

          1.235E2


          As to why you are seeing your current behavior, the # placeholders are optional digits, which means that DecimalFormat is not obligated to actually use them exactly as you used them in the pattern. The only requirement appears to be that the total number of digits appearing in the scientific notation output matches. In this case, the total number of digits is five, so we get the output 1.2346E2.






          share|improve this answer































            2














            Seems like this pattern will work as you expect



            DecimalFormat eNotation1 = new DecimalFormat("0.000E0");





            share|improve this answer


























            • Yes DecimalFormat("0.000E0") will print 1.235E2, but I'm still wondering why the output wasn't 1.235E2 with the code segment given in the question.

              – Lenovo Genovia
              53 mins ago






            • 1





              Because the difference between # and 0 in decimal Format is in existing of this digit. If you use # here there are several options of presenting this number and that's why it will work wrongly with some numbers.

              – Naya
              51 mins ago





















            2














            In order to fit your first pattern better i would just remove the first #. Like this you fit international unit system while keeping optional numbers after point.



            DecimalFormat eNotation1 = new DecimalFormat("0.###E0");   
            System.out.println(eNotation1.format(123.456789));





            share|improve this answer
























              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55903959%2fwhy-was-the-pattern-string-not-followed-in-this-code%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              4














              If you want that exact format, then use the pattern 0.000E0:



              DecimalFormat eNotation1 = new DecimalFormat("0.000E0");
              System.out.println(eNotation1.format(123.456789));

              1.235E2


              As to why you are seeing your current behavior, the # placeholders are optional digits, which means that DecimalFormat is not obligated to actually use them exactly as you used them in the pattern. The only requirement appears to be that the total number of digits appearing in the scientific notation output matches. In this case, the total number of digits is five, so we get the output 1.2346E2.






              share|improve this answer




























                4














                If you want that exact format, then use the pattern 0.000E0:



                DecimalFormat eNotation1 = new DecimalFormat("0.000E0");
                System.out.println(eNotation1.format(123.456789));

                1.235E2


                As to why you are seeing your current behavior, the # placeholders are optional digits, which means that DecimalFormat is not obligated to actually use them exactly as you used them in the pattern. The only requirement appears to be that the total number of digits appearing in the scientific notation output matches. In this case, the total number of digits is five, so we get the output 1.2346E2.






                share|improve this answer


























                  4












                  4








                  4







                  If you want that exact format, then use the pattern 0.000E0:



                  DecimalFormat eNotation1 = new DecimalFormat("0.000E0");
                  System.out.println(eNotation1.format(123.456789));

                  1.235E2


                  As to why you are seeing your current behavior, the # placeholders are optional digits, which means that DecimalFormat is not obligated to actually use them exactly as you used them in the pattern. The only requirement appears to be that the total number of digits appearing in the scientific notation output matches. In this case, the total number of digits is five, so we get the output 1.2346E2.






                  share|improve this answer













                  If you want that exact format, then use the pattern 0.000E0:



                  DecimalFormat eNotation1 = new DecimalFormat("0.000E0");
                  System.out.println(eNotation1.format(123.456789));

                  1.235E2


                  As to why you are seeing your current behavior, the # placeholders are optional digits, which means that DecimalFormat is not obligated to actually use them exactly as you used them in the pattern. The only requirement appears to be that the total number of digits appearing in the scientific notation output matches. In this case, the total number of digits is five, so we get the output 1.2346E2.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 54 mins ago









                  Tim BiegeleisenTim Biegeleisen

                  242k13103163




                  242k13103163

























                      2














                      Seems like this pattern will work as you expect



                      DecimalFormat eNotation1 = new DecimalFormat("0.000E0");





                      share|improve this answer


























                      • Yes DecimalFormat("0.000E0") will print 1.235E2, but I'm still wondering why the output wasn't 1.235E2 with the code segment given in the question.

                        – Lenovo Genovia
                        53 mins ago






                      • 1





                        Because the difference between # and 0 in decimal Format is in existing of this digit. If you use # here there are several options of presenting this number and that's why it will work wrongly with some numbers.

                        – Naya
                        51 mins ago


















                      2














                      Seems like this pattern will work as you expect



                      DecimalFormat eNotation1 = new DecimalFormat("0.000E0");





                      share|improve this answer


























                      • Yes DecimalFormat("0.000E0") will print 1.235E2, but I'm still wondering why the output wasn't 1.235E2 with the code segment given in the question.

                        – Lenovo Genovia
                        53 mins ago






                      • 1





                        Because the difference between # and 0 in decimal Format is in existing of this digit. If you use # here there are several options of presenting this number and that's why it will work wrongly with some numbers.

                        – Naya
                        51 mins ago
















                      2












                      2








                      2







                      Seems like this pattern will work as you expect



                      DecimalFormat eNotation1 = new DecimalFormat("0.000E0");





                      share|improve this answer















                      Seems like this pattern will work as you expect



                      DecimalFormat eNotation1 = new DecimalFormat("0.000E0");






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited 50 mins ago









                      Pshemo

                      96.6k15135194




                      96.6k15135194










                      answered 1 hour ago









                      NayaNaya

                      693515




                      693515













                      • Yes DecimalFormat("0.000E0") will print 1.235E2, but I'm still wondering why the output wasn't 1.235E2 with the code segment given in the question.

                        – Lenovo Genovia
                        53 mins ago






                      • 1





                        Because the difference between # and 0 in decimal Format is in existing of this digit. If you use # here there are several options of presenting this number and that's why it will work wrongly with some numbers.

                        – Naya
                        51 mins ago





















                      • Yes DecimalFormat("0.000E0") will print 1.235E2, but I'm still wondering why the output wasn't 1.235E2 with the code segment given in the question.

                        – Lenovo Genovia
                        53 mins ago






                      • 1





                        Because the difference between # and 0 in decimal Format is in existing of this digit. If you use # here there are several options of presenting this number and that's why it will work wrongly with some numbers.

                        – Naya
                        51 mins ago



















                      Yes DecimalFormat("0.000E0") will print 1.235E2, but I'm still wondering why the output wasn't 1.235E2 with the code segment given in the question.

                      – Lenovo Genovia
                      53 mins ago





                      Yes DecimalFormat("0.000E0") will print 1.235E2, but I'm still wondering why the output wasn't 1.235E2 with the code segment given in the question.

                      – Lenovo Genovia
                      53 mins ago




                      1




                      1





                      Because the difference between # and 0 in decimal Format is in existing of this digit. If you use # here there are several options of presenting this number and that's why it will work wrongly with some numbers.

                      – Naya
                      51 mins ago







                      Because the difference between # and 0 in decimal Format is in existing of this digit. If you use # here there are several options of presenting this number and that's why it will work wrongly with some numbers.

                      – Naya
                      51 mins ago













                      2














                      In order to fit your first pattern better i would just remove the first #. Like this you fit international unit system while keeping optional numbers after point.



                      DecimalFormat eNotation1 = new DecimalFormat("0.###E0");   
                      System.out.println(eNotation1.format(123.456789));





                      share|improve this answer




























                        2














                        In order to fit your first pattern better i would just remove the first #. Like this you fit international unit system while keeping optional numbers after point.



                        DecimalFormat eNotation1 = new DecimalFormat("0.###E0");   
                        System.out.println(eNotation1.format(123.456789));





                        share|improve this answer


























                          2












                          2








                          2







                          In order to fit your first pattern better i would just remove the first #. Like this you fit international unit system while keeping optional numbers after point.



                          DecimalFormat eNotation1 = new DecimalFormat("0.###E0");   
                          System.out.println(eNotation1.format(123.456789));





                          share|improve this answer













                          In order to fit your first pattern better i would just remove the first #. Like this you fit international unit system while keeping optional numbers after point.



                          DecimalFormat eNotation1 = new DecimalFormat("0.###E0");   
                          System.out.println(eNotation1.format(123.456789));






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered 26 mins ago









                          PopHipPopHip

                          1037




                          1037






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55903959%2fwhy-was-the-pattern-string-not-followed-in-this-code%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Ponta tanko

                              Tantalo (mitologio)

                              Erzsébet Schaár