Skip to main content
Share Your Experience: Take the 2024 Developer Survey
51 votes
Accepted

Which is the current decimal separator?

Ask locale: locale decimal_point This will output the decimal point using the current locale settings. If you need the thousands separator: locale thousands_sep You can view all the numeric keywords ...
Stephen Kitt's user avatar
27 votes
Accepted

Can bash do floating-point arithmetic without using an external command?

No. Bash cannot perform floating point arithmetic natively. This is not what you're looking for but may help someone else: Alternatives bc bc allows floating point arithmetic, and can even ...
jesse_b's user avatar
  • 37.4k
14 votes
Accepted

Float operations with bc not precise?

You need to understand the meaning of the scale of an expression in bc. bc can do arbitrary precision (which doesn't necessarily mean infinite precision) while your calculator will probably have the ...
Stéphane Chazelas's user avatar
12 votes
Accepted

How to drop decimal place on numbers in a JSON array?

Just running it through the identity filter with jq reformats the numbers that has a .0 decimal as integers: $ jq . file.json [ { "IMSKU": "1000001", "AttributeID": 7332, "Value": "...
Kusalananda's user avatar
  • 337k
11 votes

Why does $((0.1)) expand to 0.10000000000000001 in zsh?

TL;DR zsh chooses a decimal representation for the double binary numbers that it uses for evaluating floating point arithmetics that preserves their information fully, that is safe for reinput into ...
Stéphane Chazelas's user avatar
9 votes

How to convert floating point number to integer?

A very simple hacky one is function float_to_int() { echo $1 | cut -d. -f1 # or use -d, if decimals separator is , } Sample output $ float_to_int 32.333 32 $ float_to_int 32 32
GMaster's user avatar
  • 6,392
8 votes

How to convert floating point number to integer?

The previous answer submitted was almost correct: "You could do: float=1.23 int=${float%.*} But that would remove the fractional part instead of giving you the nearest integer and that wouldn't work ...
Jay Mayers's user avatar
8 votes

Evaluation an expression and rounding up to three decimals

Just write this: read exp printf %.3f $(echo "$exp" | bc -l)
Amit24x7's user avatar
  • 666
7 votes
Accepted

Convert exponential notation to decimal number in awk

With GNU awk: gawk -v RS='[-+]?[0-9.]+[eE][-+]?[0-9]+' \ -v ORS= \ -v CONVFMT=%.1000g '{print $0 (RT == "" ? "" : +RT)}' gawk stores what is matched by the record separator into the RT ...
Stéphane Chazelas's user avatar
6 votes
Accepted

seq decimal separator

To exclude errors in the locales themselves, I (re)generated both de_DE.UTF-8 and en_US.UTF-8 via dpkg-reconfigure locales and now seq behaviour seems to be dependent on LC_NUMERIC AND LANG. If ...
FelixJN's user avatar
  • 13.6k
6 votes

Which is the current decimal separator?

If that's a zsh shell script, you can use the $langinfo special associative array in the zsh/langinfo module: zmodload zsh/langinfo radix=$langinfo[RADIXCHAR] (that maps to the standard nl_langinfo(...
Stéphane Chazelas's user avatar
5 votes
Accepted

Bash shell decimal test

You can use the == operator of bc. test "$(echo $x == $z |bc)" -eq 1 expr1 == expr2 The result is 1 if expr1 is equal to expr2. -- from man bc(1)
mjy's user avatar
  • 596
5 votes
Accepted

Checking that a decimal number is in a range in bc

Unlike for example python, bc does not support chained comparisons: a < b < c To perform both comparisons and require both to be true, use logical-and (requires GNU bc): (a < b) && ...
John1024's user avatar
  • 74.9k
5 votes
Accepted

Convert bash loop index to floating-point

Use bc or awk or a shell with floating point support like ksh93, zsh or yash instead of bash. bc $ bc -l << \EOF heredoc> for (i = 0; i <= 10; i += 2.5) i / 2 heredoc> EOF 0 1....
Stéphane Chazelas's user avatar
5 votes
Accepted

Floating point numbers in bash

bash does not do floating-point arithmetic, but rather fixed-point, where the decimal is fixed at zero places (i. e. integer maths). This means that you can work around it for some very very basic ...
DopeGhoti's user avatar
  • 76.5k
5 votes

How to shorten decimal output in bash?

Try: $ ERRORS=1153 TOTAL=43525 $ echo "scale = 3; $ERRORS * 100 / $TOTAL" | bc 2.649 instead. See how in this case, scale = 3 is being fed to bc to set the scale variable of bc, which is ...
Stéphane Chazelas's user avatar
4 votes

Decimal to Hexadecimal conversion working in Linux but not in Unix

You didn't tell us what is your Unix and your shell. The problem lies probably in the leading 0 which instructs printf to interpret the number as an octal number, which it can't be (due to the 8 and 9 ...
xhienne's user avatar
  • 17.9k
4 votes

Convert a float to the next integer up as opposed to the nearest?

for positive integers: a=305 b=15 echo $((a%b?a/b+1:a/b)) 21
vxtal's user avatar
  • 41
4 votes
Accepted

Why do newer versions of awk print large integers instead of decimals?

Use printf instead. calc(){ awk "BEGIN{ printf (\"%.5e\\n\", $*) }" ;}
daisy's user avatar
  • 54.8k
4 votes

Convert a textual representation of a float to actual numeral using command line

Would this do? dash$ printf "%.99f\n" 3.472727272727276 3.472727272727276215391611913219094276428222656250000000000000000000000000000000000000000000000000000 or dash$ printf "%.99f\n&...
ilkkachu's user avatar
  • 140k
4 votes
Accepted

Invalid arithmetic operator (error token is " 7.16.3 when passing float from read command

-ne does a numeric comparison, while 7.16.3 is not a number. (Even if it's called a "version number".) As it happens, as far as Bash is concerned, neither would 7.16 be, since Bash only ...
ilkkachu's user avatar
  • 140k
4 votes

Replicating floating point arithmetic WITHOUT bc utility

If I understand your code correct, you want to log a message when the first value of /proc/loadavg is greater than CRIT_LOAD. This check including the parsing of /proc/loadavg can be done with a ...
Bodo's user avatar
  • 6,088
3 votes

How to compare two float values in ksh?

Per the POSIX standard for bc: Unlike all other operators, the relational operators ( '<', '>', "<=", ">=", "==", "!=" ) shall be only valid as the object of an if, while, or inside a for ...
Jeff Schaller's user avatar
  • 67.5k
3 votes
Accepted

Only allow floating points regex

grep -xE '[-+]?[0123456789]*\.?[0123456789]+([eE][-+]?[0123456789]+)?' With -x, we're anchoring the regexp at the start and and of the line so the lines have to match that pattern as a whole as ...
Stéphane Chazelas's user avatar
3 votes

Can bash do floating-point arithmetic without using an external command?

"Can bash also do floating-point arithmetic without using an external command?" Nope. robert@pip2:/tmp$ echo $((2.5 * 3)) bash: 2.5 * 3: syntax error: invalid arithmetic operator (error token is ".5 ...
mrflash818's user avatar
3 votes

Why is LDBL_MAX 1.18973E+4932 and how is this possible?

You are probably used to thinking of numbers as some number times some power of 10. Floating point numbers are represented as some number times some power of 2. You can represent 102410 (...
Fox's user avatar
  • 8,193
3 votes

Floating point numbers in bash

bash cannot do decimal point mathematical operations, only integer operations robert@pip2:/tmp$ echo $((2 * 3)) 6 robert@pip2:/tmp$ echo $((2 * 3.5)) bash: 2 * 3.5: syntax error: invalid arithmetic ...
mrflash818's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible