在PHP中从右移小数 [英] Moving decimal from the right in PHP

查看:30
本文介绍了在PHP中从右移小数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多数字,还有它们的小数点.以下是一些示例以及应该返回的内容:

I have large numbers and their decimals points. Here are some examples and what should return:

Number: 300000
Decimals: 8
Should return: 0.00300000

Number: 700000000
Decimals: 8 
Should return: 7


Number: 800000000
Decimals: 6
Should return: 800

此刻我正在这样做:

substr($number, 0, abs($decimals + 1))

但是,如果小数位数大于数字中的位数,则返回0(示例返回0).

But it returns 0 if the number of decimals is superior to the number of digits in the number (example one returns 0).

有什么想法吗?

推荐答案

除以10就是小数位数的幂:

Divide by 10 to the power of the number of decimals:

$result = $number / (10 ** $decimals);

这篇关于在PHP中从右移小数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆