长数字文本,整型、浮点或十六进制,其在代码或配置文件中的可读性较差:

1
2
3
4
5
parameters:
    credit_card_number: 1234567890123456
    long_number: 10000000000
    pi: 3.141592653589793
    hex_words: 0xCAFEF00D

在Symfony 3.2中,YAML文件增加了对“下划线”等的支持,专门用于改善数字文本的可读性:

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

在解析YAML内容的过程中,所有_字符将被从数字文本中移除,因此对于你引入下划线的数量,或是划分内容段落的方式,并无特殊要求。

这项功能是被YAML协议所定义,因此也受到其他编程语言(Java,Ruby,Rust,Swift等)的广泛支持。

弱化浮点中的逗号 

全新下划线分隔符的引入,令我们反思用逗号分隔浮点内容的必要性。最终,我们决定弱化它,因此从Symfony 3.2开始,应该减少用逗号分隔内容:

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