0

I'm Just fetching values from MySQL Database in php and in return it just neglecting .00 in last and converting float values in integer in php. but in my database all values are in float.. see my database screenshot enter image description here

my php code below

    public function GetSchemes(){
    include "../includes/config.php";
    
    if($_SERVER['REQUEST_METHOD'] == 'POST') {

        $sql = "SELECT * FROM ticket_info";   
        
        $post = $this->get_list_result($sql);
                $respon = array(
                    'status' => 'ok','scheme' => $post
                );
                $this->response($this->json($respon), 200);  
    }
    
}

   public function get_list_result($query) {
    $result = array();
    $r = $this->mysqli->query($query) or die($this->mysqli->error.__LINE__);
    if($r->num_rows > 0) {
        while($row = $r->fetch_assoc()) {
            $result[] = $row;
        }
    }
    return $result;
}

and I'm getting these values

{
"status": "ok",
"scheme": [
    {
        "id": 1,
        "ticket_type": "Bumper",
        "ticket_name": "p1",
        "mrp": 250,
        "selling_price": 225,
        "series": 2,
        "booklet": 20
    },
    {
        "id": 2,
        "ticket_type": "Bumper",
        "ticket_name": "p2",
        "mrp": 200,
        "selling_price": 190,
        "series": 10,
        "booklet": 1
    },
    {
        "id": 5,
        "ticket_type": "Daily",
        "ticket_name": "p3",
        "mrp": 6,
        "selling_price": 5.5,
        "series": 5,
        "booklet": 5
    },
    {
        "id": 6,
        "ticket_type": "Daily",
        "ticket_name": "p4",
        "mrp": 6,
        "selling_price": 5.5,
        "series": 25,
        "booklet": 1
    },
    {
        "id": 7,
        "ticket_type": "Daily",
        "ticket_name": "p5",
        "mrp": 6,
        "selling_price": 5.5,
        "series": 50,
        "booklet": 1
    },
    {
        "id": 8,
        "ticket_type": "Daily",
        "ticket_name": "p6",
        "mrp": 6,
        "selling_price": 5.5,
        "series": 100,
        "booklet": 1
    },
    {
        "id": 9,
        "ticket_type": "Daily",
        "ticket_name": "p7",
        "mrp": 6,
        "selling_price": 5.5,
        "series": 200,
        "booklet": 1
    }
]

}

See in API Result mrp should be 250.00 but its showing 250 and selling_price should be 225.00 but its showing 225 i.e .00 in last is missing... but In my database.. .00 values are saved..

Why PHP is neglecting .00 values at last... I want to use .00 values

Thanks

Álvaro González
  • 142,137
  • 41
  • 261
  • 360
bharat
  • 11
  • 5
  • 1
    Haven't tried it but might look at `JSON_PRESERVE_ZERO_FRACTION ` for `json_encode`. – AbraCadaver Oct 22 '20 at 23:02
  • tried this `$this->response($this->json_encode($respon,JSON_PRESERVE_ZERO_FRACTION), 200);` but did not worked :( – bharat Oct 22 '20 at 23:13
  • How did it __did not worked__ – AbraCadaver Oct 22 '20 at 23:23
  • It is a very bad idea to use `die(mysqli_error($conn));` in your code, because it could potentially leak sensitive information. See this post for more explanation: [mysqli or die, does it have to die?](https://stackoverflow.com/a/15320411/1839439) – Dharman Oct 23 '20 at 11:14

2 Answers2

1

Float values are represented in JSON without any formatting. If you want to keep the formatting, you'll need to use number_format() or sprintf() and convert the floats to strings before using json_encode().

while ($row = $r->fetch_assoc()) {
    $row['mrp'] = number_format($row['mrp'], 2);
    $row['selling_price'] = number_format($row['selling_price'], 2);
    $result[] = $row;
}

You can use JSON_PRESERVE_ZERO_FRACTION with json_encode(), but that will just ensure the number is encoded as a float, not apply formatting with a specific number of decimal places, so for numbers with .00, you'll just see .0 in the JSON.

I'm referring to trailing zeroes as formatting because as far as a float is concerned, 250.00 and 250 are the same thing.

Don't Panic
  • 41,125
  • 10
  • 61
  • 80
  • 1
    Exactly. JSON is based in JavaScript and JavaScript only had one numeric type at the time (see [JavaScript data types](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_and_Structure_types)). In JavaScript, `225.00 === 225` is `true` because it's the same exact type. – Álvaro González Oct 23 '20 at 15:00
0

Verify the serialize-precision and precision directives in the php.ini file. You may need to configure this value.

https://www.php.net/manual/en/ini.core.php#ini.serialize-precision https://www.php.net/manual/en/ini.core.php#ini.precision

Also you can try to pass those values to string, enclosing them in double quotes.

julian0018
  • 33
  • 1
  • 6