Skip to content

Float values lose decimal point when rendered #235

@ryo-morimoto

Description

@ryo-morimoto

Overview

When a math filter returns a float value with no fractional part (e.g., 2.0), PHP Liquid renders it as "2" instead of "2.0". This differs from Ruby Shopify/Liquid, which preserves the decimal notation for float values.

Examples

Template Ruby Liquid PHP Liquid
{{ 1 | plus: 1.0 }} 2.0 2
{{ "1" | plus: "1.0" }} 2.0 2
{{ 2.0 | times: 1 }} 2.0 2
{{ 10 | divided_by: 4.0 }} 2.5 2.5

Note: When there is a fractional part (like 2.5), both implementations produce the same output.

Minimal Reproduction

Ruby Liquid

require 'liquid'

template = Liquid::Template.parse('{{ 1 | plus: 1.0 }}')
puts template.render
# => "2.0"

PHP Liquid

<?php
require 'vendor/autoload.php';

use Liquid\Template;

$template = new Template();
$template->parse('{{ 1 | plus: 1.0 }}');
echo $template->render();
// => "2"

Root Cause

PHP automatically converts float(2.0) to "2" when casting to string:

$f = 2.0;
echo (string)$f;  // "2"

$f = 2.5;
echo (string)$f;  // "2.5"

Refs

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions