Negative floating point numbers are not sorted correctly using awk or sort

南笙酒味 提交于 2020-05-11 12:54:26

问题


For some reason, comparisons of negative floating point numbers with awk and sort seem to be broken on my machine. It seems that -0.1 < -0.2.

When I try to sort

0.2
-0.1
-0.2
0.1
0

using sort -n test.dat, I get

-0.1
-0.2
0
0.1
0.2

instead of

-0.2
-0.1
0
0.1
0.2

What is wrong with me?


回答1:


Answer: You are French!

In french, the decimal point is a comma (,) and not a dot (.). You need to either replace the dots with commas or change your locale.

Try LC_NUMERIC=us_EN.UTF-8 sort -n test.dat and you should get the expected result.

For your information, LC_NUMERIC is an environment variable that contains the locale to use for formatting non-monetary numbers.



来源:https://stackoverflow.com/questions/19985823/negative-floating-point-numbers-are-not-sorted-correctly-using-awk-or-sort

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!