shell 浮点数转换为整数。用bc的scale实现

linux下的bc命令可以设置结果的位数,通过 scale.
比如:
$ echo “scale=4; 1.2323293128 / 1.1” | bc -l
1.1202
但是scale只对除法、取余、乘幂有效,比如乘法就无效:
$ echo “scale=4; 1.2323293128 * 1.1” | bc -l
1.3555622440
想了一个回避的方法,就是除以1。
比如:
$ echo “scale=4; (1.2323293128 * 1.1) / 1” | bc -l
1.3555

root@host109:/opt# echo “scale=0; l b a n u m 1 0 ∗ {lba_num_10}* lbanum10{t_rate}”|bc
7501465944.0
root@host109:/opt# echo “scale=1; l b a n u m 1 0 ∗ {lba_num_10}* lbanum10{t_rate}”|bc
7501465944.0
root@host109:/opt# echo “scale=2; l b a n u m 1 0 ∗ {lba_num_10}* lbanum10{t_rate}”|bc
7501465944.0
root@host109:/opt# echo “scale=2;KaTeX parse error: Expected ‘EOF’, got ‘#’ at position 52: …ot@host109:/opt#̲ echo “scale=2;…{lba_num_10}”|bc
7501465944.0
root@host109:/opt# echo “scale=0;0.5*KaTeX parse error: Expected ‘EOF’, got ‘#’ at position 48: …ot@host109:/opt#̲ echo “scale=0;…{lba_num_10}”
scale=0;0.515002931888
root@host109:/opt# echo “scale=0;0.5
KaTeX parse error: Expected ‘EOF’, got ‘#’ at position 50: …ot@host109:/opt#̲ echo “scale=0;{lba_num_10}*${t_rate}/1″|bc
7501465944
root@host109:/opt#

    原文作者:旅行者6号
    原文地址: https://blog.csdn.net/weixin_44631767/article/details/116758675
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞