Let's first treat numbers as numbers and use bc
if it's available:
$ t=13.491675
$ read ts fr < <(printf 't=%s; print t/1," ",t-t/1\n' "$t" | bc)
$ echo "$ts + $fr = $t"
13 + .491675 = 13.491675
The same can be acomplished with the computing capabilities of awk
:
$ read ts fr < <(echo "$t" | awk '{printf("%d %f\n", int($1), $1-int($1))}')
$ echo "$ts + $fr = $t"
13 + 0.491675 = 13.491675
Note the extra leading 0
in the fractional part.
Or a mixture of printf
to extract the integer part and bc
to extract the fractional part:
$ t=13.491675
$ printf -v ts '%.0f\n' "$t"
$ fr=$(printf '%s-%d\n' "$t" "$i" | bc)
$ echo "$ts + $fr = $t"
13 + .491675 = 13.491675
Or, if bc
is not an option, printf
and bash parameter expansion to finish the job:
$ t=13.491675
$ printf -v ts '%.0f\n' "$t"
$ fr="${t#$i}"
$ echo "$ts + $fr = $t"
13 + .491675 = 13.491675
To get rid of the decimal separator, if needed, we can use one more parameter expansion:
$ fr="${fr:1}"
or:
$ fr="${fr:2}"
for the awk
-based solution.