How to move decimal point in bash
![Creative The name of the picture](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgO9GURib1T8z7lCwjOGLQaGtrueEthgQ8LO42ZX8cOfTqDK4jvDDpKkLFwf2J49kYCMNW7d4ABih_XCb_2UXdq5fPJDkoyg7-8g_YfRUot-XnaXkNYycsNp7lA5_TW9td0FFpLQ2APzKcZ/s1600/1.jpg)
![Creative The name of the picture](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhYQ0N5W1qAOxLP7t7iOM6O6AzbZnkXUy16s7P_CWfOb5UbTQY_aDsc727chyphenhyphen5W4IppVNernMMQeaUFTB_rFzAd95_CDt-tnwN-nBx6JyUp2duGjPaL5-VgNO41AVsA_vu30EJcipdDG409/s400/Clash+Royale+CLAN+TAG%2523URR8PPP.png)
up vote
3
down vote
favorite
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?
bash
add a comment |Â
up vote
3
down vote
favorite
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?
bash
1
do you need to change it to0.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 bexEy
orx ⢠10^y
, where1 < x < 10
andy
is the power of ten.
â juniorRubyist
Jun 7 at 4:19
add a comment |Â
up vote
3
down vote
favorite
up vote
3
down vote
favorite
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?
bash
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?
bash
edited Jun 6 at 18:16
asked Jun 6 at 18:04
J. Doe
937
937
1
do you need to change it to0.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 bexEy
orx ⢠10^y
, where1 < x < 10
andy
is the power of ten.
â juniorRubyist
Jun 7 at 4:19
add a comment |Â
1
do you need to change it to0.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 bexEy
orx ⢠10^y
, where1 < x < 10
andy
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
add a comment |Â
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
add a comment |Â
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
add a comment |Â
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
add a comment |Â
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
add a comment |Â
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
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
edited Jun 6 at 22:34
![](https://i.stack.imgur.com/9L8vd.png?s=32&g=1)
![](https://i.stack.imgur.com/9L8vd.png?s=32&g=1)
dessert
19.4k55494
19.4k55494
answered Jun 6 at 18:29
glenn jackman
11.7k2241
11.7k2241
add a comment |Â
add a comment |Â
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
add a comment |Â
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
add a comment |Â
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
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
edited Jun 6 at 18:50
answered Jun 6 at 18:16
![](https://i.stack.imgur.com/9L8vd.png?s=32&g=1)
![](https://i.stack.imgur.com/9L8vd.png?s=32&g=1)
dessert
19.4k55494
19.4k55494
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
orx ⢠10^y
, where1 < x < 10
andy
is the power of ten.â juniorRubyist
Jun 7 at 4:19