我想在 php 中显示一个数字,它在小数点后有 30 位数字,而不将其四舍五入为纳米货币,有人有什么想法吗?
I want to show a number in php which has 30 digits after decimal without rounding it up for nano currency, anyone have any Idea?
例如,如果我有一个号码
$x = 340282366920938430000000000000000000000;
我想要输出为
340282366.92093 而不是 340282366.92094
还有像
这样的数字
$y = 23000000000;
我希望输出是
0.000000000000000000023
使用字符串转换 bcdiv
$number = '340282366920938430000000000000000000000';
echo bcdiv($number, '1000000000000000000000000000000', 8);
Return
340282366.92093843
您需要做的是将 raws 转换为 megaNano。我开发了这个解决方案,它只是将值视为一个字符串,但它是在 JS 中。我希望这可以帮助你的 Nano 项目!
function toMegaNano(raws) {
raws = raws.toString()
let megaNano
if (raws == "0") return "0"
if ((raws.length - 30) > 0) {
megaNano = raws.substr(0, raws.length - 30)
fraction = raws.substr(raws.length - 30, raws.length - (raws.length - 30))
if (fraction.length && parseInt(fraction) != 0) megaNano += '.' + fraction
} else {
megaNano = "0." + "0".repeat(30 - raws.length) + raws
}
while (megaNano[megaNano.length - 1] == '0') {
megaNano = megaNano.substr(0, megaNano.length - 1)
}
return megaNano
}
const raws = "340282366920938430000000000000000000000"
const mNano = toMegaNano(raws)
console.log(raws + " raws = " + mNano + " mNano")
例如,如果我有一个号码
$x = 340282366920938430000000000000000000000;
我想要输出为 340282366.92093 而不是 340282366.92094 还有像
这样的数字$y = 23000000000;
我希望输出是 0.000000000000000000023
使用字符串转换 bcdiv
$number = '340282366920938430000000000000000000000';
echo bcdiv($number, '1000000000000000000000000000000', 8);
Return
340282366.92093843
您需要做的是将 raws 转换为 megaNano。我开发了这个解决方案,它只是将值视为一个字符串,但它是在 JS 中。我希望这可以帮助你的 Nano 项目!
function toMegaNano(raws) {
raws = raws.toString()
let megaNano
if (raws == "0") return "0"
if ((raws.length - 30) > 0) {
megaNano = raws.substr(0, raws.length - 30)
fraction = raws.substr(raws.length - 30, raws.length - (raws.length - 30))
if (fraction.length && parseInt(fraction) != 0) megaNano += '.' + fraction
} else {
megaNano = "0." + "0".repeat(30 - raws.length) + raws
}
while (megaNano[megaNano.length - 1] == '0') {
megaNano = megaNano.substr(0, megaNano.length - 1)
}
return megaNano
}
const raws = "340282366920938430000000000000000000000"
const mNano = toMegaNano(raws)
console.log(raws + " raws = " + mNano + " mNano")