Floating-point division in bash

前端 未结 4 2029
隐瞒了意图╮
隐瞒了意图╮ 2021-01-21 14:42

I\'m trying to convert whatever numbers the user inputs into 2 decimal places. For instance

What is the total cost in cents? 2345
output: 23.45

4条回答
  •  面向向阳花
    2021-01-21 15:32

    How about this:

    read -p "What is the total cost? " input
    percent=20
    echo "scale=2; $input / 100 * $percent / 100" | bc
    
    # input = 2345 , output = 4.69
    

提交回复
热议问题