dongwei4096 2017-03-14 16:10
浏览 79

PostgreSQL中“Money”字段的意外行为

I started developing a web sistem using Linux Ubuntu and at some point I had to do the following with the data type "money":

explode(" ", "R$ 3,000.00"); // [0] => "R$" and [1] => "3,000.00"

However when I installed the software in Windows I realized that the data is saved without space, that is, "R$3,000.00". Soon, the code snippet fails to function properly.

Note: 1 could "fix" this using:

preg_replace("/[R$]+/", "$0 $1", "R$3,000.00"); // "R$ 3,000.00"

But certainly not a better way.

Note 2: The version of PostgreSQL used is 9.5

Would anyone have any suggestions for resolving this?

Thank you very much.

  • 写回答

1条回答 默认 最新

  • dongshou6041 2017-03-14 17:36
    关注

    The issue you are having is that the lc_monetary locale does not have the same value on both computers. This is what you have an "Unexpected behavior" on two different operating systems.

    You can change the lc_monetary locale with:

    set lc_monetary to 'SOME_LOCALE';
    

    Then test it with:

    test=# SELECT 34.888::money;
     money  
    --------
     $34.89
    (1 row)
    

    Read more at https://www.postgresql.org/docs/current/static/runtime-config-client.html#GUC-LC-MONETARY

    If your application is using different OS, it is wise to set the locale correctly at the beginning of the connection or in the configuration.

    On Mac/Linux you can see available locales with locale -a. I an not sure for Windows.

    If you don't generally use the currency symbol you should definitively consider to store the number as a decimal instead.

    评论

报告相同问题?

悬赏问题

  • ¥15 我的数据无法存进链表里
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端