-1

For example, if I have a number

$x = 340282366920938430000000000000000000000;

I want output as 340282366.92093 and not 340282366.92094 Also for a number like

$y = 23000000000;

I want the output to be 0.000000000000000000023

2 Answers2

1

use string cast an bcdiv

$number = '340282366920938430000000000000000000000';
echo bcdiv($number, '1000000000000000000000000000000', 8);

Return

340282366.92093843
Monnomcjo
  • 715
  • 1
  • 4
  • 14
0

What you need to do is convert raws to megaNano. I had developed this solution that simply treats the values as a string, but it is in JS. I hope this can help your project with 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")
Anarkrypto
  • 81
  • 1
  • 4