Moving decimal from the right in PHP

落爺英雄遲暮 提交于 2021-02-05 09:23:09

问题


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

At the moment I'm doing this:

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

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

Any idea how to do this?


回答1:


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

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



回答2:


Divide by ten to the power of decimals?

$n = 700000000;
$d = 8;

Echo $n/pow(10,$d);

https://3v4l.org/MdoW1



来源:https://stackoverflow.com/questions/47143473/moving-decimal-from-the-right-in-php

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