Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Why is this happening? Can I prevent this? (besides passing them as string)

var_dump(json_encode([1002.31, 2002.42]));

outputs:

string(39) "[1002.3099999999999,2002.4200000000001]"
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
368 views
Welcome To Ask or Share your Answers For Others

1 Answer

You should configure 'precision' and 'serialize_precision' params.

precision = 14
serialize_precision = -1

Test case:

php -r 'var_dump(json_encode([1002.31, 2002.42]));'
string(39) "[1002.3099999999999,2002.4200000000001]"

php -r 'ini_set("precision", 14); ini_set("serialize_precision", -1); var_dump(json_encode([1002.31, 2002.42]));'
string(17) "[1002.31,2002.42]"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...