Convert a big integer to a full string in PHP
I've been searching for a while now, but what I can find is not what I search for. I need to convert an integer value, that may be very huge, to a string. Sounds easy: "$var"
? No, because this can lead to the E+
representation of the number.
<?php
$var = 10000000000000000000000000;
echo $var."n";
echo "'$var'n";
echo (string) $var."n";
echo strval($var);
?>
1.0E+25
'1.0E+25'
1.0E+25
1.0E+25
How can I make the output be 10000000000000000000000000
instead?
php casting integer type-conversion
add a comment |
I've been searching for a while now, but what I can find is not what I search for. I need to convert an integer value, that may be very huge, to a string. Sounds easy: "$var"
? No, because this can lead to the E+
representation of the number.
<?php
$var = 10000000000000000000000000;
echo $var."n";
echo "'$var'n";
echo (string) $var."n";
echo strval($var);
?>
1.0E+25
'1.0E+25'
1.0E+25
1.0E+25
How can I make the output be 10000000000000000000000000
instead?
php casting integer type-conversion
3
Please note that your "very huge integer" is not represented in your computer as an integer, as it exceeds both 32bit and 64bit bounds.
– thetaiko
Mar 8 '12 at 17:14
1
$var = '10000000000000000000000000';
– Ryan Kempt
Mar 8 '12 at 17:15
You cannot do this with a native int value. It will have to be stored as a string. If you're doing math operations on this number, you'll have to use the bcmath library.
– Marc B
Mar 8 '12 at 17:23
Note that if you are decoding large integers from JSON, you can useJSON_BIGINT_AS_STRING
as the 4th argument tojson_decode
– jchook
May 6 '16 at 18:04
add a comment |
I've been searching for a while now, but what I can find is not what I search for. I need to convert an integer value, that may be very huge, to a string. Sounds easy: "$var"
? No, because this can lead to the E+
representation of the number.
<?php
$var = 10000000000000000000000000;
echo $var."n";
echo "'$var'n";
echo (string) $var."n";
echo strval($var);
?>
1.0E+25
'1.0E+25'
1.0E+25
1.0E+25
How can I make the output be 10000000000000000000000000
instead?
php casting integer type-conversion
I've been searching for a while now, but what I can find is not what I search for. I need to convert an integer value, that may be very huge, to a string. Sounds easy: "$var"
? No, because this can lead to the E+
representation of the number.
<?php
$var = 10000000000000000000000000;
echo $var."n";
echo "'$var'n";
echo (string) $var."n";
echo strval($var);
?>
1.0E+25
'1.0E+25'
1.0E+25
1.0E+25
How can I make the output be 10000000000000000000000000
instead?
php casting integer type-conversion
php casting integer type-conversion
asked Mar 8 '12 at 17:09
Niklas RNiklas R
6,9771663147
6,9771663147
3
Please note that your "very huge integer" is not represented in your computer as an integer, as it exceeds both 32bit and 64bit bounds.
– thetaiko
Mar 8 '12 at 17:14
1
$var = '10000000000000000000000000';
– Ryan Kempt
Mar 8 '12 at 17:15
You cannot do this with a native int value. It will have to be stored as a string. If you're doing math operations on this number, you'll have to use the bcmath library.
– Marc B
Mar 8 '12 at 17:23
Note that if you are decoding large integers from JSON, you can useJSON_BIGINT_AS_STRING
as the 4th argument tojson_decode
– jchook
May 6 '16 at 18:04
add a comment |
3
Please note that your "very huge integer" is not represented in your computer as an integer, as it exceeds both 32bit and 64bit bounds.
– thetaiko
Mar 8 '12 at 17:14
1
$var = '10000000000000000000000000';
– Ryan Kempt
Mar 8 '12 at 17:15
You cannot do this with a native int value. It will have to be stored as a string. If you're doing math operations on this number, you'll have to use the bcmath library.
– Marc B
Mar 8 '12 at 17:23
Note that if you are decoding large integers from JSON, you can useJSON_BIGINT_AS_STRING
as the 4th argument tojson_decode
– jchook
May 6 '16 at 18:04
3
3
Please note that your "very huge integer" is not represented in your computer as an integer, as it exceeds both 32bit and 64bit bounds.
– thetaiko
Mar 8 '12 at 17:14
Please note that your "very huge integer" is not represented in your computer as an integer, as it exceeds both 32bit and 64bit bounds.
– thetaiko
Mar 8 '12 at 17:14
1
1
$var = '10000000000000000000000000';
– Ryan Kempt
Mar 8 '12 at 17:15
$var = '10000000000000000000000000';
– Ryan Kempt
Mar 8 '12 at 17:15
You cannot do this with a native int value. It will have to be stored as a string. If you're doing math operations on this number, you'll have to use the bcmath library.
– Marc B
Mar 8 '12 at 17:23
You cannot do this with a native int value. It will have to be stored as a string. If you're doing math operations on this number, you'll have to use the bcmath library.
– Marc B
Mar 8 '12 at 17:23
Note that if you are decoding large integers from JSON, you can use
JSON_BIGINT_AS_STRING
as the 4th argument to json_decode
– jchook
May 6 '16 at 18:04
Note that if you are decoding large integers from JSON, you can use
JSON_BIGINT_AS_STRING
as the 4th argument to json_decode
– jchook
May 6 '16 at 18:04
add a comment |
4 Answers
4
active
oldest
votes
This is not stored as an integer by PHP, but a float, this is why you end up with 1.0E+25 instead of 10000000000000000000000000.
It's sadly not possible to use that as an integer value in PHP, as PHP cannot save an integer of that size. If this comes from database then it will be a string and you can do with it whatever you want. If you store it elsewhere then store it as a string.
Your alternative is to store it as a float and take that into account at all times, though that requires additional conversions and handling in places.
It's also been suggested to use GNU Multiple Precision, but that's not enabled in PHP by default.
$int=gmp_init("10000000000000000000000000");
$string=gmp_strval($int);
echo $string;
I was just about to suggest GMP. Don't use a float - the precision at that large of a number isn't reliable.
– AndrewR
Mar 8 '12 at 17:30
add a comment |
UPDATE:
Found the next post:
// strval() will lose digits around pow(2,45);
echo pow(2,50); // 1.1258999068426E+015
echo (string)pow(2,50); // 1.1258999068426E+015
echo strval(pow(2,50)); // 1.1258999068426E+015
// full conversion
printf('%0.0f',pow(2,50)); // 112589906846624
echo sprintf('%0.0f',pow(2,50)); // 112589906846624
Use printf
or sprintf
.
I submit it in mistake before adding the second part of the answer
– Ofir Baruch
Mar 8 '12 at 17:17
Floats aren't reliable at this size.echo sprintf("%0.0f", 10000000000000000000000000);
on my system outputs10000000000000000905969664
.
– AndrewR
Mar 8 '12 at 17:36
add a comment |
The integer number you like to express:
$var = 10000000000000000000000000;
is not available on your system. It's too large and therefore PHP converts it into a float which will change the number (32 bit system example):
10000000000000000905969664
Common limits are:
yours : 10 000 000 000 000 000 000 000 000
32 bit: 2 147 483 648
64 bit: 9 223 372 036 854 775 808
The change of the value is called floating point precision, the PHP manual about integers will tell you about the integer limit and the floats page about floating point precision (see the big red warning). Depending on which system you are, you can compile PHP with the ranges your application needs or you must use another datatype, for example with the gmp library which is able to pick strings as integer numbers and handle them.
The following example shows just output, but you can do multiplications etc.:
$r = gmp_init('10000000000000000000000000');
echo gmp_strval($r);
Hope this is helpful.
add a comment |
I'm facing this problem when getting facebook id and find it in MySQL.
And after half hour, i found this work perfectly!
Insert this line to your php script:
ini_set('precision',30);
From: https://forums.phpfreaks.com/topic/125907-solved-convert-big-number-to-string/#entry651084
After spending Whole day i found this tip. it save my life. Thanks Duo.
– user3535066
Nov 6 '17 at 6:07
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f9621792%2fconvert-a-big-integer-to-a-full-string-in-php%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
This is not stored as an integer by PHP, but a float, this is why you end up with 1.0E+25 instead of 10000000000000000000000000.
It's sadly not possible to use that as an integer value in PHP, as PHP cannot save an integer of that size. If this comes from database then it will be a string and you can do with it whatever you want. If you store it elsewhere then store it as a string.
Your alternative is to store it as a float and take that into account at all times, though that requires additional conversions and handling in places.
It's also been suggested to use GNU Multiple Precision, but that's not enabled in PHP by default.
$int=gmp_init("10000000000000000000000000");
$string=gmp_strval($int);
echo $string;
I was just about to suggest GMP. Don't use a float - the precision at that large of a number isn't reliable.
– AndrewR
Mar 8 '12 at 17:30
add a comment |
This is not stored as an integer by PHP, but a float, this is why you end up with 1.0E+25 instead of 10000000000000000000000000.
It's sadly not possible to use that as an integer value in PHP, as PHP cannot save an integer of that size. If this comes from database then it will be a string and you can do with it whatever you want. If you store it elsewhere then store it as a string.
Your alternative is to store it as a float and take that into account at all times, though that requires additional conversions and handling in places.
It's also been suggested to use GNU Multiple Precision, but that's not enabled in PHP by default.
$int=gmp_init("10000000000000000000000000");
$string=gmp_strval($int);
echo $string;
I was just about to suggest GMP. Don't use a float - the precision at that large of a number isn't reliable.
– AndrewR
Mar 8 '12 at 17:30
add a comment |
This is not stored as an integer by PHP, but a float, this is why you end up with 1.0E+25 instead of 10000000000000000000000000.
It's sadly not possible to use that as an integer value in PHP, as PHP cannot save an integer of that size. If this comes from database then it will be a string and you can do with it whatever you want. If you store it elsewhere then store it as a string.
Your alternative is to store it as a float and take that into account at all times, though that requires additional conversions and handling in places.
It's also been suggested to use GNU Multiple Precision, but that's not enabled in PHP by default.
$int=gmp_init("10000000000000000000000000");
$string=gmp_strval($int);
echo $string;
This is not stored as an integer by PHP, but a float, this is why you end up with 1.0E+25 instead of 10000000000000000000000000.
It's sadly not possible to use that as an integer value in PHP, as PHP cannot save an integer of that size. If this comes from database then it will be a string and you can do with it whatever you want. If you store it elsewhere then store it as a string.
Your alternative is to store it as a float and take that into account at all times, though that requires additional conversions and handling in places.
It's also been suggested to use GNU Multiple Precision, but that's not enabled in PHP by default.
$int=gmp_init("10000000000000000000000000");
$string=gmp_strval($int);
echo $string;
answered Mar 8 '12 at 17:28
kingmaplekingmaple
2,57422339
2,57422339
I was just about to suggest GMP. Don't use a float - the precision at that large of a number isn't reliable.
– AndrewR
Mar 8 '12 at 17:30
add a comment |
I was just about to suggest GMP. Don't use a float - the precision at that large of a number isn't reliable.
– AndrewR
Mar 8 '12 at 17:30
I was just about to suggest GMP. Don't use a float - the precision at that large of a number isn't reliable.
– AndrewR
Mar 8 '12 at 17:30
I was just about to suggest GMP. Don't use a float - the precision at that large of a number isn't reliable.
– AndrewR
Mar 8 '12 at 17:30
add a comment |
UPDATE:
Found the next post:
// strval() will lose digits around pow(2,45);
echo pow(2,50); // 1.1258999068426E+015
echo (string)pow(2,50); // 1.1258999068426E+015
echo strval(pow(2,50)); // 1.1258999068426E+015
// full conversion
printf('%0.0f',pow(2,50)); // 112589906846624
echo sprintf('%0.0f',pow(2,50)); // 112589906846624
Use printf
or sprintf
.
I submit it in mistake before adding the second part of the answer
– Ofir Baruch
Mar 8 '12 at 17:17
Floats aren't reliable at this size.echo sprintf("%0.0f", 10000000000000000000000000);
on my system outputs10000000000000000905969664
.
– AndrewR
Mar 8 '12 at 17:36
add a comment |
UPDATE:
Found the next post:
// strval() will lose digits around pow(2,45);
echo pow(2,50); // 1.1258999068426E+015
echo (string)pow(2,50); // 1.1258999068426E+015
echo strval(pow(2,50)); // 1.1258999068426E+015
// full conversion
printf('%0.0f',pow(2,50)); // 112589906846624
echo sprintf('%0.0f',pow(2,50)); // 112589906846624
Use printf
or sprintf
.
I submit it in mistake before adding the second part of the answer
– Ofir Baruch
Mar 8 '12 at 17:17
Floats aren't reliable at this size.echo sprintf("%0.0f", 10000000000000000000000000);
on my system outputs10000000000000000905969664
.
– AndrewR
Mar 8 '12 at 17:36
add a comment |
UPDATE:
Found the next post:
// strval() will lose digits around pow(2,45);
echo pow(2,50); // 1.1258999068426E+015
echo (string)pow(2,50); // 1.1258999068426E+015
echo strval(pow(2,50)); // 1.1258999068426E+015
// full conversion
printf('%0.0f',pow(2,50)); // 112589906846624
echo sprintf('%0.0f',pow(2,50)); // 112589906846624
Use printf
or sprintf
.
UPDATE:
Found the next post:
// strval() will lose digits around pow(2,45);
echo pow(2,50); // 1.1258999068426E+015
echo (string)pow(2,50); // 1.1258999068426E+015
echo strval(pow(2,50)); // 1.1258999068426E+015
// full conversion
printf('%0.0f',pow(2,50)); // 112589906846624
echo sprintf('%0.0f',pow(2,50)); // 112589906846624
Use printf
or sprintf
.
answered Mar 8 '12 at 17:15
Ofir BaruchOfir Baruch
8,90411833
8,90411833
I submit it in mistake before adding the second part of the answer
– Ofir Baruch
Mar 8 '12 at 17:17
Floats aren't reliable at this size.echo sprintf("%0.0f", 10000000000000000000000000);
on my system outputs10000000000000000905969664
.
– AndrewR
Mar 8 '12 at 17:36
add a comment |
I submit it in mistake before adding the second part of the answer
– Ofir Baruch
Mar 8 '12 at 17:17
Floats aren't reliable at this size.echo sprintf("%0.0f", 10000000000000000000000000);
on my system outputs10000000000000000905969664
.
– AndrewR
Mar 8 '12 at 17:36
I submit it in mistake before adding the second part of the answer
– Ofir Baruch
Mar 8 '12 at 17:17
I submit it in mistake before adding the second part of the answer
– Ofir Baruch
Mar 8 '12 at 17:17
Floats aren't reliable at this size.
echo sprintf("%0.0f", 10000000000000000000000000);
on my system outputs 10000000000000000905969664
.– AndrewR
Mar 8 '12 at 17:36
Floats aren't reliable at this size.
echo sprintf("%0.0f", 10000000000000000000000000);
on my system outputs 10000000000000000905969664
.– AndrewR
Mar 8 '12 at 17:36
add a comment |
The integer number you like to express:
$var = 10000000000000000000000000;
is not available on your system. It's too large and therefore PHP converts it into a float which will change the number (32 bit system example):
10000000000000000905969664
Common limits are:
yours : 10 000 000 000 000 000 000 000 000
32 bit: 2 147 483 648
64 bit: 9 223 372 036 854 775 808
The change of the value is called floating point precision, the PHP manual about integers will tell you about the integer limit and the floats page about floating point precision (see the big red warning). Depending on which system you are, you can compile PHP with the ranges your application needs or you must use another datatype, for example with the gmp library which is able to pick strings as integer numbers and handle them.
The following example shows just output, but you can do multiplications etc.:
$r = gmp_init('10000000000000000000000000');
echo gmp_strval($r);
Hope this is helpful.
add a comment |
The integer number you like to express:
$var = 10000000000000000000000000;
is not available on your system. It's too large and therefore PHP converts it into a float which will change the number (32 bit system example):
10000000000000000905969664
Common limits are:
yours : 10 000 000 000 000 000 000 000 000
32 bit: 2 147 483 648
64 bit: 9 223 372 036 854 775 808
The change of the value is called floating point precision, the PHP manual about integers will tell you about the integer limit and the floats page about floating point precision (see the big red warning). Depending on which system you are, you can compile PHP with the ranges your application needs or you must use another datatype, for example with the gmp library which is able to pick strings as integer numbers and handle them.
The following example shows just output, but you can do multiplications etc.:
$r = gmp_init('10000000000000000000000000');
echo gmp_strval($r);
Hope this is helpful.
add a comment |
The integer number you like to express:
$var = 10000000000000000000000000;
is not available on your system. It's too large and therefore PHP converts it into a float which will change the number (32 bit system example):
10000000000000000905969664
Common limits are:
yours : 10 000 000 000 000 000 000 000 000
32 bit: 2 147 483 648
64 bit: 9 223 372 036 854 775 808
The change of the value is called floating point precision, the PHP manual about integers will tell you about the integer limit and the floats page about floating point precision (see the big red warning). Depending on which system you are, you can compile PHP with the ranges your application needs or you must use another datatype, for example with the gmp library which is able to pick strings as integer numbers and handle them.
The following example shows just output, but you can do multiplications etc.:
$r = gmp_init('10000000000000000000000000');
echo gmp_strval($r);
Hope this is helpful.
The integer number you like to express:
$var = 10000000000000000000000000;
is not available on your system. It's too large and therefore PHP converts it into a float which will change the number (32 bit system example):
10000000000000000905969664
Common limits are:
yours : 10 000 000 000 000 000 000 000 000
32 bit: 2 147 483 648
64 bit: 9 223 372 036 854 775 808
The change of the value is called floating point precision, the PHP manual about integers will tell you about the integer limit and the floats page about floating point precision (see the big red warning). Depending on which system you are, you can compile PHP with the ranges your application needs or you must use another datatype, for example with the gmp library which is able to pick strings as integer numbers and handle them.
The following example shows just output, but you can do multiplications etc.:
$r = gmp_init('10000000000000000000000000');
echo gmp_strval($r);
Hope this is helpful.
edited Mar 8 '12 at 17:36
answered Mar 8 '12 at 17:30
hakrehakre
160k32303616
160k32303616
add a comment |
add a comment |
I'm facing this problem when getting facebook id and find it in MySQL.
And after half hour, i found this work perfectly!
Insert this line to your php script:
ini_set('precision',30);
From: https://forums.phpfreaks.com/topic/125907-solved-convert-big-number-to-string/#entry651084
After spending Whole day i found this tip. it save my life. Thanks Duo.
– user3535066
Nov 6 '17 at 6:07
add a comment |
I'm facing this problem when getting facebook id and find it in MySQL.
And after half hour, i found this work perfectly!
Insert this line to your php script:
ini_set('precision',30);
From: https://forums.phpfreaks.com/topic/125907-solved-convert-big-number-to-string/#entry651084
After spending Whole day i found this tip. it save my life. Thanks Duo.
– user3535066
Nov 6 '17 at 6:07
add a comment |
I'm facing this problem when getting facebook id and find it in MySQL.
And after half hour, i found this work perfectly!
Insert this line to your php script:
ini_set('precision',30);
From: https://forums.phpfreaks.com/topic/125907-solved-convert-big-number-to-string/#entry651084
I'm facing this problem when getting facebook id and find it in MySQL.
And after half hour, i found this work perfectly!
Insert this line to your php script:
ini_set('precision',30);
From: https://forums.phpfreaks.com/topic/125907-solved-convert-big-number-to-string/#entry651084
answered Apr 7 '17 at 5:04
kblekble
10818
10818
After spending Whole day i found this tip. it save my life. Thanks Duo.
– user3535066
Nov 6 '17 at 6:07
add a comment |
After spending Whole day i found this tip. it save my life. Thanks Duo.
– user3535066
Nov 6 '17 at 6:07
After spending Whole day i found this tip. it save my life. Thanks Duo.
– user3535066
Nov 6 '17 at 6:07
After spending Whole day i found this tip. it save my life. Thanks Duo.
– user3535066
Nov 6 '17 at 6:07
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f9621792%2fconvert-a-big-integer-to-a-full-string-in-php%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
3
Please note that your "very huge integer" is not represented in your computer as an integer, as it exceeds both 32bit and 64bit bounds.
– thetaiko
Mar 8 '12 at 17:14
1
$var = '10000000000000000000000000';
– Ryan Kempt
Mar 8 '12 at 17:15
You cannot do this with a native int value. It will have to be stored as a string. If you're doing math operations on this number, you'll have to use the bcmath library.
– Marc B
Mar 8 '12 at 17:23
Note that if you are decoding large integers from JSON, you can use
JSON_BIGINT_AS_STRING
as the 4th argument tojson_decode
– jchook
May 6 '16 at 18:04