EasyAdmin 数字字段
此字段用于表示存储任何类型数字(整数或小数)的属性值。
在表单页面(编辑和新建)中,它看起来像这样

基本信息
- PHP 类:
EasyCorp
\Bundle \EasyAdminBundle \Field \NumberField - 用于存储此值的 Doctrine DBAL 类型:
decimal
,float
或string
- 用于渲染字段的 Symfony 表单类型: NumberType
渲染为:
1 2
<!-- the type of element used is configurable --> <input type="number"> or <input type="text">
选项
setDecimalSeparator
数值显示 PHP 使用的默认十进制分隔符(例如,1/10 显示为 0.1
)。使用此选项设置不同的字符来分隔数字的小数部分
1 2
// this would display '12345.67' as '12345,67'
yield NumberField::new('...')->setDecimalSeparator(',');
setNumberFormat
默认情况下,数字“按原样”显示。如果您希望以任何方式格式化该值,请使用此选项并传递任何有效的格式化字符串作为 sprintf()
函数的参数
1 2
// this would display numbers in scientific notation (e.g. 123456.7890 = '1.234568e+5')
yield NumberField::new('...')->setNumberFormat('%e');
注意
使用此选项将使 EasyAdmin 忽略选项 setNumDecimals
和 setRoundingMode
。
setNumDecimals
默认情况下,数字“按原样”显示,不会添加或删除任何小数位。如果您想使用特定的小数位数格式化值,请使用此选项
1 2
// this would format 3 as 3.00 and 5.123 as 5.12
yield NumberField::new('...')->setNumDecimals(2);
setRoundingMode
默认情况下,当某个值必须四舍五入以减少小数位数时,该字段使用 PHP \NumberFormatter::ROUND_HALFUP
策略。使用此选项更改舍入策略,并将其参数作为 PHP NumberFormatter 类的任何 ROUND_*
常量传递
1
yield NumberField::new('...')->setRoundingMode(\NumberFormatter::ROUND_CEILING);
setStoredAsString
默认情况下,此字段假定您将值存储为数字属性。如果您将值存储为字符串(例如,因为它是一个非常大的数字),请使用此选项也显示一个 <input type="text">
元素,而不是默认的 <input type="number">
元素
1
yield NumberField::new('...')->setStoredAsString();
setThousandsSeparator
默认情况下,数值不会以任何方式分隔千位分组(例如,12345.67
像这样显示,而不是 12,345.67
)。使用此选项设置用于分隔每个千位分组的字符
1 2
// this would display '12345.67' as '12 345.67'
yield NumberField::new('...')->setThousandsSeparator(' ');
本作品,包括代码示例,根据 Creative Commons BY-SA 3.0 许可协议获得许可。