float php 下取_php float 不四舍五入截取浮点型字符串

php中截取浮点型大致有下面几种方法:

1 float round ( float $val [, int $precision ] ) 返回将 val 根据指定精度 precision (十进制小数点后数字的数目)进行四舍五入的结果。precision 也可以是负数或零(默认值)。

echo round(4.3)  //4

2 string sprintf (

string $format [, $args [,

$a=12.338938438;

echo sprintf("%.5f",$a) //结果:12.33894

$a=12.3312356;

echo sprintf("%.5f",$a);//12.33124

echo sprintf("%f",$a);//331236 默认小数点后6位

3 string number_format ( float $number , int $decimals , string $dec_point , string $thousands_sep )

$number = 1234.5678;

$english_format_number = number_format($number, 2, '.', '');

echo $english_format_number ; // 1234.57

以上这些都自动做了四舍五入,有时候需求不需要四舍五入呢,怎么办,没有想到好办法,谁知道可以告诉一声。

自己写了个麻烦点的函数,记录下

function getFloatValue($f,$len)

{

$tmpInt=intval($f);

$tmpDecimal=$f-$tmpInt;

$str="$tmpDecimal";

$subStr=strstr($str,'.');

if(strlen($subStr)

{

$repeatCount=$len+1-strlen($subStr);

$str=$str."".str_repeat("0",$repeatCount);

}

return $tmpInt."".substr($str,1,1+$len);

}

echo getFloatValue(12.99,4) //12.9900

echo getFloatValue(12.9232555553239,4) //12.9232

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值