Search code examples
bashawkgnumultiple-versions

sprintf behaves differently in GNU Awk 3.1.5 and GNU Awk 3.17


Consider the following command in a bash terminal :

echo 0.00027849383762390271 | awk '{print sprintf("%.20f",$1)}'

I'm connected to two machines.

The first has GNU Bash version 4.15 and GNU Awk version 3.1.7. The above command returns what is expected : 0.00027849383762390268 (although the last two digits are different from my initial number)

The second machine has GNU Bash version 3.2.39 and GNU Awk version 3.1.5. The above command returns : 0,00000000000000000000 (20 zeros after the comma). Note the ',' instead of a '.' .

Why does the second machine behave differently than the first, and how to make it behaves like the first machine ?


Solution

  • Your locale is different between the 2 machines. Set LC_ALL=C (or whatever locale you want) before executing both commands and see https://www.gnu.org/software/gawk/manual/html_node/Locale-influences-conversions.html for more information.