Closed BSarmady closed 3 years ago
Need decimal as well !
@bobsort your methods fail. I have this value in my JSON / MySQL : 42.1234567890123456 using your method I get : 42.123456789012
So it is missing the whole value. Seems like its a float which is not what I wanted. I mean I would then just use a float right? But I need precision of the decimal data type.
Nevermind. My fault. Didn't check the input.
I was doing this : echo json_encode($mydata, JSON_NUMERIC_CHECK ); and this cut off decimal places.
echo json_encode($mydata); works as expected (side effect is every number is encapsulated with " )
works as expected (side effect is every number is encapsulated with " )
This is exactly what the JSON standard is suggesting. The JSON standard does not have any requirement on the precision of numbers. Double is the most widely used implementation. If you must ensure to not loose any precision you should store your value as string and parse it yourself the way you need it.
SimpleJSON was mainly written for the use in the Unity 3d engine. You barely need more precision than a single precision float. It wouldn't make much sense to implement conversion operators / properties for every possible type out there. If you need a certain conversion you can write an extension for your needs. All classes are declared as partial so you can simply add new methods, operators or properties with an extending class.
Anyway his decimal function works
I've added a seperate extension file for .NET types including decimal, DateTime, TimeSpan, Guid and some others in the latest update. 029f124f8fc7dfc2db50e605a0f0bebfcccb58bd
This should close the gap. I don't want to add too many automatic conversions to the base framework as it will just blow up the size of the code. The extension files just need to be placed alongside the main file. (partial class abuse :) ).
Converting to DateTime and Decimal types are missing, I suggest following code but I'm not sure if it will work correctly.