New in Symfony 3.2: Better readability for YAML numeric literals
July 11, 2016 • Published by Javier Eguiluz
Warning: This post is about an unsupported Symfony version. Some of this information may be out of date. Read the most recent Symfony Docs.
Contributed by
Baptiste Clavié
in #18486.
Long numeric literals, being integer, float or hexadecimal, are known for their poor readability in code and configuration files:
1 2 3 4 5
parameters:
credit_card_number: 1234567890123456
long_number: 10000000000
pi: 3.141592653589793
hex_words: 0xCAFEF00D
In Symfony 3.2, YAML files added support for including underscores in numeric literals to improve their readability:
1 2 3 4 5
parameters:
credit_card_number: 1234_5678_9012_3456
long_number: 10_000_000_000
pi: 3.14159_26535_89793
hex_words: 0x_CAFE_F00D
During the parsing of the YAML contents, all the _
characters are removed
from the numeric literal contents, so there is not a limit in the number of
underscores you can include or the way you group contents.
This feature is defined in the YAML specification and it's widely supported in other programming languages (Java, Ruby, Rust, Swift, etc.).
Deprecating comma separators in floats
Contributed by
Christian Flothmann
in #18785.
The new underscore separator made us think about the need of the comma to separate the float number contents. Ultimately we decided to deprecate it, so starting from Symfony 3.2, the use of the comma to separate contents is deprecated:
1 2 3 4 5 6 7 8 9
parameters:
# deprecated since Symfony 3.2
foo: 1,230.15
# equivalent without the comma separator
foo: 1230.15
# equivalent with the underscore separator
foo: 1_230.15
Help the Symfony project!
As with any Open-Source project, contributing code or documentation is the most common way to help, but we also have a wide range of sponsoring opportunities.
Comments are closed.
To ensure that comments stay relevant, they are closed for old posts.
Is there a way to escape the underscore sign? In case I would like to keep it.