微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php – 如何在MySQL中存储欧洲货币?

在我工作的新项目中,我有CSV格式的数据导入到mysql表中.其中一列是以欧洲格式存储货币的价格字段,即. 345,83.
我有的是存储这个十进制分隔符.在大多数欧洲货币中,十进制分隔符是“,”但是当我尝试在字段中插入十进制数字(例如345,83)时,我收到以下错误:“数据在行’行’列’column_name’被截断# “”.如果我使用’.’而不是’,’它工作正常.能帮帮我,如何在MysqL中存储这种格式?

解决方法:

您可以将其存储为数据库中的常规十进制字段,并在显示时格式化欧洲数字样式

编辑:刚刚添加一个如何实现的示例

$european_numbers = array('123.345,78', '123 456,78', ',78');

foreach($european_numbers as $number) {

    echo "$number was converted to ".convert_european_to_decimal($number)."\n";
    // save in database Now
}

function convert_european_to_decimal($number) {
    // i am sure there are better was of doing this, but this is nice and simple example
    $number = str_replace('.', '', $number); // remove fullstop
    $number = str_replace(' ', '', $number); // remove spaces
    $number = str_replace(',', '.', $number); // change comma to fullstop

    return $number;
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐