Questions tagged [floating-point]

94 questions
409
votes
17 answers

How to do integer & float calculations, in bash or other languages/frameworks?

Using echo "20+5" literally produces the text "20+5". What command can I use to get the numeric sum, 25 in this case? Also, what's the easiest way to do it just using bash for floating point? For example, echo $((3224/3807.0)) prints 0 :(. I am…
Michael Durrant
  • 41,213
  • 69
  • 165
  • 232
72
votes
5 answers

How to convert floating point number to integer?

Is this the right way to do float to integer conversion in bash? Is there any other method? flotToint() { printf "%.0f\n" "$@" }
Rahul Patil
  • 24,281
  • 25
  • 80
  • 96
64
votes
4 answers

Are operations on floats supported with bc?

It looks like bc doesn't support float operations. When I do echo 1/8 | bc it gets me a zero. I checked the manual page bc (1), but it doesn't even mention float, so I wonder if it's supported?
daisy
  • 53,527
  • 78
  • 236
  • 383
33
votes
2 answers

Which is the current decimal separator?

Say I have a POSIX shell script that needs to run on different systems/environments that I do not control, and needs to remove the decimal separator from a string that is emitted by a program that respects the locale settings. How can I detect…
gboffi
  • 1,220
  • 1
  • 14
  • 30
24
votes
3 answers

How to format floating point number with exactly 2 significant digits in bash?

I want to print the floating point number with exactly two significant digits in bash (maybe using a common tool like awk, bc, dc, perl etc.). Examples: 76543 should be printed as 76000 0.0076543 should be printed as 0.0076 In both cases the…
tafit3
  • 343
  • 1
  • 2
  • 5
22
votes
3 answers

awk high precision arithmetic

I am looking for a way to tell awk to do high-precision arithmetic in a substitution operation. This involves, reading a field from a file and substituting it with a 1% increment on that value. However, I am losing precision there. Here is a…
Ketan Maheshwari
  • 9,054
  • 6
  • 40
  • 53
20
votes
3 answers

How to round floating point numbers in shell?

How do I correctly round IEEE 754 floating point numbers on the command line? I want to specify the precision of the output number - the count of fractional digits. Rounding 6.66 to precision 1 should give 6.7, for example. More in the table…
Volker Siegel
  • 16,983
  • 5
  • 52
  • 79
13
votes
2 answers

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

I have read that bash can do integer arithmetic without using an external command, for example: echo "$((3 * (2 + 1)))" Can bash also do floating-point arithmetic without using an external command?
user267288
  • 133
  • 1
  • 1
  • 4
12
votes
4 answers

bash loop with 0.02 increment

I want to make a for loop in bash with 0.02 as increments I tried for ((i=4.00;i<5.42;i+=0.02)) do commands done but it didn't work.
Mehrshad
  • 121
  • 1
  • 1
  • 3
10
votes
5 answers

Bash limiting precision of floating point variables

In Ubuntu 14.04.1 LTS 64-bit bash I am declearing floating point variables by multiplying floating point bash variables in bc with scale set to 3; however, I cannot get the number of digits after the decimal point to be zero and get rid of the zero…
Vesnog
  • 679
  • 4
  • 11
  • 29
9
votes
2 answers

Comparison of decimal numbers in bash

My search this morning was about how could I compare two decimal numbers in bash, and I came to this answser: How to compare to floating point number in a shell script. This one, however, doesn't include this answer here: $ [[ ((3.56 < 2.90)) ]];…
admirabilis
  • 4,642
  • 9
  • 41
  • 57
9
votes
4 answers

How to round or convert a float value to int with bc? getting: "(standard_in) 1: syntax error"

I will get value like 2743410360.320 and I want value like 2743410360 to a variable. I tried INTValueOfGB=$ echo "($gb+0.5)/1" | bc But I am getting (standard_in) 1: syntax error
vin
  • 91
  • 1
  • 1
  • 2
8
votes
1 answer

Is it possible to get a decimal output from doing division in Bash?

Basically in Bash, what I'm trying to do is take a input of seconds from a user and find the hours. So basically if the user enter 35 the output should be 0.00972222. But bash gives me is a zero. heres the command I have: echo "Enter the seconds you…
shawn edward
  • 473
  • 3
  • 13
  • 17
8
votes
4 answers

Weird float rounding behavior with printf

I've read some answers on this site and found the printf rounding desirable. However when I used it in practice, a subtle bug led me to the following behavior: $ echo 197.5 | xargs printf '%.0f' 198 $ echo 196.5 | xargs printf '%.0f' 196 $ echo…
Hai Zhang
  • 185
  • 1
  • 8
6
votes
1 answer

seq decimal separator

Using a seq command with floating point numbers, my output comes with commas instead of dots as decimal separators, despite using dots in the input: seq 0.1 0.3 1.3 0,1 0,4 0,7 1,0 1,3 I assumed this to be linked with the locale LC_NUMERIC, which…
FelixJN
  • 12,616
  • 2
  • 27
  • 48
1
2 3 4 5 6 7