Cast string with comma to float with dot

喜你入骨 提交于 2019-12-13 19:50:27

问题


When I try to cast

$value = floatval('14,5833');

to a float type I expect a value with dot like 14.5833 but it returns me 14,5833.

How should I do this ?

I wouldn't like to use any string replace functions.


回答1:


You have two options:

  • Set locale to something that uses a dot instead of a coma. E.g. setLocale(LC_ALL, 'fr_BE.UTF-8');
  • Keep using comma internally, and when you want to output that number, use number_format



回答2:


An example was helpful for me:

<?php
setlocale(LC_NUMERIC, 'en_US');
echo 1.234; // 1.234
setlocale(LC_NUMERIC, 'et_EE.UTF-8');
echo 1.234; // 1,234
echo number_format( 1.234, 2, '.', '' ); // 1.23 
?>



回答3:


Check decimal_point from localeconv and setlocale



来源:https://stackoverflow.com/questions/2996743/cast-string-with-comma-to-float-with-dot

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