How to move decimal point in bash

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP








up vote
3
down vote

favorite
1












I have a variable that is stored as "1.20E20". I would like for it to be changed to "0.0120E22". Is there a simple command to change this value? I tried



let nh=$nh/100


But that did not work; I assume it is because of the character value in the variable. I know there are various solutions; what is the best way to solve this?







share|improve this question

















  • 1




    do you need to change it to 0.120E21? Do you want to divide it by ten, or just express the same value with a different form?
    – glenn jackman
    Jun 6 at 18:11











  • @glennjackman Yes, I need it to change to 0.012E22 (I messed up slightly in the original question, but you were right in assuming).
    – J. Doe
    Jun 6 at 18:20










  • Technically, a number in scientific notation is supposed to be xEy or x • 10^y, where 1 < x < 10 and y is the power of ten.
    – juniorRubyist
    Jun 7 at 4:19














up vote
3
down vote

favorite
1












I have a variable that is stored as "1.20E20". I would like for it to be changed to "0.0120E22". Is there a simple command to change this value? I tried



let nh=$nh/100


But that did not work; I assume it is because of the character value in the variable. I know there are various solutions; what is the best way to solve this?







share|improve this question

















  • 1




    do you need to change it to 0.120E21? Do you want to divide it by ten, or just express the same value with a different form?
    – glenn jackman
    Jun 6 at 18:11











  • @glennjackman Yes, I need it to change to 0.012E22 (I messed up slightly in the original question, but you were right in assuming).
    – J. Doe
    Jun 6 at 18:20










  • Technically, a number in scientific notation is supposed to be xEy or x • 10^y, where 1 < x < 10 and y is the power of ten.
    – juniorRubyist
    Jun 7 at 4:19












up vote
3
down vote

favorite
1









up vote
3
down vote

favorite
1






1





I have a variable that is stored as "1.20E20". I would like for it to be changed to "0.0120E22". Is there a simple command to change this value? I tried



let nh=$nh/100


But that did not work; I assume it is because of the character value in the variable. I know there are various solutions; what is the best way to solve this?







share|improve this question













I have a variable that is stored as "1.20E20". I would like for it to be changed to "0.0120E22". Is there a simple command to change this value? I tried



let nh=$nh/100


But that did not work; I assume it is because of the character value in the variable. I know there are various solutions; what is the best way to solve this?









share|improve this question












share|improve this question




share|improve this question








edited Jun 6 at 18:16
























asked Jun 6 at 18:04









J. Doe

937




937







  • 1




    do you need to change it to 0.120E21? Do you want to divide it by ten, or just express the same value with a different form?
    – glenn jackman
    Jun 6 at 18:11











  • @glennjackman Yes, I need it to change to 0.012E22 (I messed up slightly in the original question, but you were right in assuming).
    – J. Doe
    Jun 6 at 18:20










  • Technically, a number in scientific notation is supposed to be xEy or x • 10^y, where 1 < x < 10 and y is the power of ten.
    – juniorRubyist
    Jun 7 at 4:19












  • 1




    do you need to change it to 0.120E21? Do you want to divide it by ten, or just express the same value with a different form?
    – glenn jackman
    Jun 6 at 18:11











  • @glennjackman Yes, I need it to change to 0.012E22 (I messed up slightly in the original question, but you were right in assuming).
    – J. Doe
    Jun 6 at 18:20










  • Technically, a number in scientific notation is supposed to be xEy or x • 10^y, where 1 < x < 10 and y is the power of ten.
    – juniorRubyist
    Jun 7 at 4:19







1




1




do you need to change it to 0.120E21? Do you want to divide it by ten, or just express the same value with a different form?
– glenn jackman
Jun 6 at 18:11





do you need to change it to 0.120E21? Do you want to divide it by ten, or just express the same value with a different form?
– glenn jackman
Jun 6 at 18:11













@glennjackman Yes, I need it to change to 0.012E22 (I messed up slightly in the original question, but you were right in assuming).
– J. Doe
Jun 6 at 18:20




@glennjackman Yes, I need it to change to 0.012E22 (I messed up slightly in the original question, but you were right in assuming).
– J. Doe
Jun 6 at 18:20












Technically, a number in scientific notation is supposed to be xEy or x • 10^y, where 1 < x < 10 and y is the power of ten.
– juniorRubyist
Jun 7 at 4:19




Technically, a number in scientific notation is supposed to be xEy or x • 10^y, where 1 < x < 10 and y is the power of ten.
– juniorRubyist
Jun 7 at 4:19










2 Answers
2






active

oldest

votes

















up vote
6
down vote



accepted










I don't know of a way to force printf to shift the exponent. Let's do it manually: multiply the coefficient by 10-2, and add 2 to the exponent.



n=1.20E20
m=2
IFS="E" read coeff exp <<<"$n"
new=$(printf "%.4fE%dn" "$(echo "$coeff * 10^-($m)" | bc -l)" "$((exp+m))")
# => 0.0120E22


We can validate with:



$ printf "%en" "$new"
1.200000e+20





share|improve this answer






























    up vote
    2
    down vote













    I’d write a simple bash script like this:



    #!/bin/bash
    a=$1%E*
    b=$1#*E
    echo 0$(<<<"scale=4;$a/10^$2" bc)E$((b+$2))


    You just need to give it the values as arguments, first the number and then the shift:



    $ bash /path/to/script 1.20E20 2
    0.0120E22
    # or, as a oneliner:
    $ bash -c 'echo 0$(<<<"scale=4;$1%E*/10^$2" bc)E$(($1#*E+$2))' _ 1.20E20 2
    0.0120E22





    share|improve this answer























      Your Answer







      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "89"
      ;
      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',
      convertImagesToLinks: true,
      noModals: false,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );








       

      draft saved


      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1044251%2fhow-to-move-decimal-point-in-bash%23new-answer', 'question_page');

      );

      Post as a guest






























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      6
      down vote



      accepted










      I don't know of a way to force printf to shift the exponent. Let's do it manually: multiply the coefficient by 10-2, and add 2 to the exponent.



      n=1.20E20
      m=2
      IFS="E" read coeff exp <<<"$n"
      new=$(printf "%.4fE%dn" "$(echo "$coeff * 10^-($m)" | bc -l)" "$((exp+m))")
      # => 0.0120E22


      We can validate with:



      $ printf "%en" "$new"
      1.200000e+20





      share|improve this answer



























        up vote
        6
        down vote



        accepted










        I don't know of a way to force printf to shift the exponent. Let's do it manually: multiply the coefficient by 10-2, and add 2 to the exponent.



        n=1.20E20
        m=2
        IFS="E" read coeff exp <<<"$n"
        new=$(printf "%.4fE%dn" "$(echo "$coeff * 10^-($m)" | bc -l)" "$((exp+m))")
        # => 0.0120E22


        We can validate with:



        $ printf "%en" "$new"
        1.200000e+20





        share|improve this answer

























          up vote
          6
          down vote



          accepted







          up vote
          6
          down vote



          accepted






          I don't know of a way to force printf to shift the exponent. Let's do it manually: multiply the coefficient by 10-2, and add 2 to the exponent.



          n=1.20E20
          m=2
          IFS="E" read coeff exp <<<"$n"
          new=$(printf "%.4fE%dn" "$(echo "$coeff * 10^-($m)" | bc -l)" "$((exp+m))")
          # => 0.0120E22


          We can validate with:



          $ printf "%en" "$new"
          1.200000e+20





          share|improve this answer















          I don't know of a way to force printf to shift the exponent. Let's do it manually: multiply the coefficient by 10-2, and add 2 to the exponent.



          n=1.20E20
          m=2
          IFS="E" read coeff exp <<<"$n"
          new=$(printf "%.4fE%dn" "$(echo "$coeff * 10^-($m)" | bc -l)" "$((exp+m))")
          # => 0.0120E22


          We can validate with:



          $ printf "%en" "$new"
          1.200000e+20






          share|improve this answer















          share|improve this answer



          share|improve this answer








          edited Jun 6 at 22:34









          dessert

          19.4k55494




          19.4k55494











          answered Jun 6 at 18:29









          glenn jackman

          11.7k2241




          11.7k2241






















              up vote
              2
              down vote













              I’d write a simple bash script like this:



              #!/bin/bash
              a=$1%E*
              b=$1#*E
              echo 0$(<<<"scale=4;$a/10^$2" bc)E$((b+$2))


              You just need to give it the values as arguments, first the number and then the shift:



              $ bash /path/to/script 1.20E20 2
              0.0120E22
              # or, as a oneliner:
              $ bash -c 'echo 0$(<<<"scale=4;$1%E*/10^$2" bc)E$(($1#*E+$2))' _ 1.20E20 2
              0.0120E22





              share|improve this answer



























                up vote
                2
                down vote













                I’d write a simple bash script like this:



                #!/bin/bash
                a=$1%E*
                b=$1#*E
                echo 0$(<<<"scale=4;$a/10^$2" bc)E$((b+$2))


                You just need to give it the values as arguments, first the number and then the shift:



                $ bash /path/to/script 1.20E20 2
                0.0120E22
                # or, as a oneliner:
                $ bash -c 'echo 0$(<<<"scale=4;$1%E*/10^$2" bc)E$(($1#*E+$2))' _ 1.20E20 2
                0.0120E22





                share|improve this answer

























                  up vote
                  2
                  down vote










                  up vote
                  2
                  down vote









                  I’d write a simple bash script like this:



                  #!/bin/bash
                  a=$1%E*
                  b=$1#*E
                  echo 0$(<<<"scale=4;$a/10^$2" bc)E$((b+$2))


                  You just need to give it the values as arguments, first the number and then the shift:



                  $ bash /path/to/script 1.20E20 2
                  0.0120E22
                  # or, as a oneliner:
                  $ bash -c 'echo 0$(<<<"scale=4;$1%E*/10^$2" bc)E$(($1#*E+$2))' _ 1.20E20 2
                  0.0120E22





                  share|improve this answer















                  I’d write a simple bash script like this:



                  #!/bin/bash
                  a=$1%E*
                  b=$1#*E
                  echo 0$(<<<"scale=4;$a/10^$2" bc)E$((b+$2))


                  You just need to give it the values as arguments, first the number and then the shift:



                  $ bash /path/to/script 1.20E20 2
                  0.0120E22
                  # or, as a oneliner:
                  $ bash -c 'echo 0$(<<<"scale=4;$1%E*/10^$2" bc)E$(($1#*E+$2))' _ 1.20E20 2
                  0.0120E22






                  share|improve this answer















                  share|improve this answer



                  share|improve this answer








                  edited Jun 6 at 18:50


























                  answered Jun 6 at 18:16









                  dessert

                  19.4k55494




                  19.4k55494






















                       

                      draft saved


                      draft discarded


























                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1044251%2fhow-to-move-decimal-point-in-bash%23new-answer', 'question_page');

                      );

                      Post as a guest













































































                      Popular posts from this blog

                      pylint3 and pip3 broken

                      Missing snmpget and snmpwalk

                      How to enroll fingerprints to Ubuntu 17.10 with VFS491