-3

I want to convert variable to float type,but when i encode it to json format it is converted to string format. This is the way I am coverting it, $amt =0.1; $amt = number_format($amt,7); Now, variable value is like : 0.1000000 And after json encoding it is converted to string type like : {"amount":"0.1000000"} But want it as : {"amount":0.1000000}

Please help , how to maintain the float type even if it is json encoded

bhayashree
  • 22
  • 6
  • 1
    `number_format` is the thing that is producing a string. Why do you need your float to be `0.1000000`?! That's exactly the same value as `0.1`. – deceze Feb 13 '19 at 10:35
  • I've closed your question as a duplicate of what I think you're *actually* asking for. Note that there's no way to have a float with trailing zeros, because it makes no sense. If you really need that, you'll have to write your own JSON serialiser. – deceze Feb 13 '19 at 11:10
  • every time the amount will not be 0.1 it may be 1, 2 or 0.03893875748, etc. So here I want it to limit upto 7 decimals and use the float type as it is in json format , but it turns to string format when encoded – bhayashree Feb 14 '19 at 07:02

1 Answers1

0
 $amt = 0.1; 
 $amt = number_format($amt,7);
 echo gettype($amt);

//string

echo 0.1000000;
//0.1;

you can use parseFloat or similar function when will get that json