Nicolas Halberstadt
Nicolas Halberstadt

Reputation: 115

Symfony can't display float number

I have a Symfony product entity mapped with Doctrine.

My column price is defined like that :

 /**
  * @ORM\Column(type="float", scale=2)
  */
 private $price;

In PhpMyAdmin, on one of my products, i define the price to be 59.99. But when i display it in my template i can't have the entire number and i have only 59. I tried doing this :


{{  product.price|number_format(2) }}

but it displays 59.00.. the dump gives me the same thing.

Does anyone have an idea ?

Upvotes: 3

Views: 1382

Answers (2)

Nicolas Halberstadt
Nicolas Halberstadt

Reputation: 115

I found the solution thanks to @AlexandreTranchant, my product.price getter was like that

public function getPrice(): ?int
{
    return $this->price;
}

so it allowed me to store only integer and not float/decimal number. Changed it to float and I have my entire decimal number.

Upvotes: 2

Frank B
Frank B

Reputation: 3697

Not sure why you loose the decimals but i prefer to use type decimal:

    /**
     * @ORM\Column(type="decimal", precision=10, scale=2)
     */
    private $price;

Upvotes: 1

Related Questions