简体   繁体   中英

An exception has been thrown during the rendering of a template ("Warning: Loss of data on string conversion")

I'm using http://php-decimal.io/

Here is my twig

<td class="text-right">{{ accDocument.taxValueSum|number_format(3, ',', ' ') }} {{ accDocument.currency.code }}</td>

and Here is my function in entity

public function getVatValue(): Decimal     
{         
    return new Decimal($this->vatValue, 3);     
}

It worked with increasing precision from 3 to 15.

public function getVatValue(): Decimal     
{         
    return new Decimal($this->vatValue, 15);     
}

From the official doc :

A warning will be raised if value was not parsed completely. For example, "0.135" to a precision of 2 will result in "0.14" with a warning. Similarly, 123 with a precision of 2 would result in 120 with a warning because data has been lost.

As my understanding, precision is related to the value length. So new Decimal($value, strlen((string) $value)) should work. In your case:

public function getVatValue(): Decimal     
{         
    return new Decimal($this->vatValue, strlen((string) $this->vatValue));     
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM