shahab bahmani
shahab bahmani

Reputation: 31

How to convert very small, normal, Large, scientific number to string in php

I used this function but it does not work good with very large and small numbers

function  convert($number)
{
return  (string)sprintf("%.40f",  floatval($number));
}
//example 1
// convert(10);
//"10.0000000000000000000000000000000000000000" correct
//
//example 2
// convert(1.0464844434049273e-9)
// "0.0000000010464844434049272962108666736101" incorrect
// "0.0000000010464844434049273" correct
//
//example 3
// convert(0.0000010464844434049273)
// "0.0000010464844434049273375698973012615234" incorrect
// "0.0000010464844434049001" correct

I checked these numbers in here

is there any good package for working with these types of number in php or laravel?

Upvotes: 3

Views: 294

Answers (1)

Bakhouche Akram
Bakhouche Akram

Reputation: 96

Some responses which it helps.

First :

Note that PHP is using a signed integer internally. The size depends on your system.

32bit system:

2^(32-1) = 2147483648 64bit system:

2^(64-1) = 9223372036854775808 -1 because 1 bit is reserved for the signage flag.

Second :

To deal with large numbers and use it as strings, try using BCMath functions.

Upvotes: 2

Related Questions