Reputation: 72560
I'm using the Symfony YAML component. If I output data from my database, numbers come up as strings so I'm converting them to the appropriate type (int or float) using filter_var
.
However, outputting floats that are whole numbers like 7.0
instead outputs !!float 7
. The 7.0
is parsed perfectly fine as a float in PHP, it only changes it on output.
Example code:
<?php
require 'vendor/autoload.php';
use Symfony\Component\Yaml\Yaml;
$yaml = 'test: { PHP: 7.0, MySQL: 5.5 }';
echo Yaml::dump(Yaml::parse($yaml));
Output:
test:
PHP: !!float 7
MySQL: 5.5
But I want:
test:
PHP: 7.0
MySQL: 5.5
Is there a way to do this? Can't see anything in the options.
Upvotes: 1
Views: 7090
Reputation: 5881
No, there is no way to influence how a floating point value is dumped. The corresponding code from the Yaml component looks like this:
if (\is_float($value)) {
$repr = (string) $value;
if (is_infinite($value)) {
$repr = str_ireplace('INF', '.Inf', $repr);
} elseif (floor($value) == $value && $repr == $value) {
// Preserve float data type since storing a whole number will result in integer value.
$repr = '!!float '.$repr;
}
} else {
$repr = \is_string($value) ? "'$value'" : (string) $value;
}
Upvotes: 2