PHP生成输入金额的分值

时间:2016-03-10 18:52:38

标签: php formatting int

在我的数据库中,我以微分存储用户数量。 现在我需要将用户输入转换为整数。

用户输入(字符串):| output(int): 1.00 100 1 100 1,51 151

我不知道,如何解决这个问题。格式化数量如:1.00和1,00不是一个大问题,但是格式化数量的最佳方式是什么,例如" 1"到适量的分钱? 我目前使用的是什么(不使用" 1" 等输入):

       $value = intval(str_replace([',','.'],'', $request->amount));

2 个答案:

答案 0 :(得分:1)

如何将其作为浮点数再乘以100?

<?php

header("Content-type: text/plain");

$x = '1,51';
$x = (float) str_replace(',','.', $x);
echo (int) ($x * 100)."\n";

$x = '1';
$x = (float) str_replace(',','.', $x);
echo (int) ($x * 100)."\n";

$x = '1.51';
$x = (float) str_replace(',','.', $x);
echo (int) ($x * 100)."\n";

$x = '1,00';
$x = (float) str_replace(',','.', $x);
echo (int) ($x * 100)."\n";

$x = '1.00';
$x = (float) str_replace(',','.', $x);
echo (int) ($x * 100)."\n";

返回:

151
100
151
100
100

答案 1 :(得分:0)

str_replace应该与两个数组或两个字符串一起使用

       $value = intval(str_replace([',','.'],['',''], $request->amount));