3

I have a big number:

2923174917395723957

that would be:

2,923*10^18

are there any parameters in bc that will give this OUTPUT?
e.g.:

$ echo '2923174917395723956 + 1' | bc
2,923*10^18
$ 

Or something similar...the point is that it must have a short look
Thank you!

6

Try the printf command:

$ printf "%e\n" 2923174917395723957
2.923175e+18

In your locale, it should use , instead of ., of course. You can also control the format more precisely such as:

$ printf "%.3e\n" 2923174917395723957
2.923e+18

Some shells like bash have a built-in called printf which may be different from any printf command that comes with the system, but, in general, you shouldn't notice any difference with simple commands like above. You can also use printf from Perl which will be pretty consistent.

$ perl -e 'printf "%.3e\n", 2923174917395723957'
2.923e+18
0

In bc numbers are represented as decimals, (in fact in base 100, one digit per byte), not the way processors do. Numbers are output as there are stored in bc internal registry. The print function as no option to print numbers with exponent. But in bc everything is possible. You can make your own function to format numbers output. You can store your own functions in a file and add the file path to the variable BC_ENV_ARGS, then functions will be available each time you invoke bc.

Check http://phodd.net/gnu-bc/code/output_formatting.bc for printfe function...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.