calc console

Ответить
admin
Администратор
Сообщения: 198
Зарегистрирован: 05 янв 2011, 04:19

calc console

Сообщение admin »

How to do calculation if I only have command line? If you have BC you can, and you can do a very complicated calculation. To perform calculation, you can type bc and start to type your question, or passing question through pipeline, such as

Код: Выделить всё

echo "56.8 + 77.7" | bc


Let say you want to convert decimal to hexadecimal, you can do this.

Код: Выделить всё

echo "obase=16; ibase=10; 56" | bc


ibase is input base, and obase is output base, by specified the base, you can convert number from one base to another.

To perform division is a bit tricky, because the will result usually in floating point. Therefore in order to get the correct answer, you need to specified scale. scale means the precision of floating point, how many digit after the point. By default the scale is 0, that means it is integer.
5.00500 – the scale is 5

5.005 – the scale is 3

So the example :

Код: Выделить всё

echo "scale=6;  60/7.02" | bc
Ответить