EasyAdmin Number Field
This field is used to represent the value of properties that store numbers of any type (integers or decimals).
In form pages (edit and new) it looks like this:
Basic Information
- PHP Class:
EasyCorp
\Bundle \EasyAdminBundle \Field \NumberField - Doctrine DBAL Type used to store this value:
decimal
,float
orstring
- Symfony Form Type used to render the field: NumberType
Rendered as:
1 2
<!-- the type of element used is configurable --> <input type="number"> or <input type="text">
Options
setDecimalSeparator
The numeric values show the default decimal separator used by PHP (e.g. 1/10 is
shown as 0.1
). Use this option to set a different character to separate the
decimal part of the number:
1 2
// this would display '12345.67' as '12345,67'
yield NumberField::new('...')->setDecimalSeparator(',');
setNumberFormat
By default, numbers are displayed "as is". If you prefer to format the value in
any way, use this option and pass any formatting string valid as an argument of
the sprintf()
function:
1 2
// this would display numbers in scientific notation (e.g. 123456.7890 = '1.234568e+5')
yield NumberField::new('...')->setNumberFormat('%e');
Caution
Using this option will make EasyAdmin to ignore the options setNumDecimals
and setRoundingMode
.
setNumDecimals
By default, numbers are displayed "as is" without adding or removing any decimals to it. Use this option if you want to format values with a certain number of decimals:
1 2
// this would format 3 as 3.00 and 5.123 as 5.12
yield NumberField::new('...')->setNumDecimals(2);
setRoundingMode
By default, when some value must be rounded to reduce the number of decimals,
the field uses PHP \NumberFormatter::ROUND_HALFUP
strategy. Use this option
to change the rounding strategy and pass as its arguments any of the ROUND_*
constants of PHP NumberFormatter class:
1
yield NumberField::new('...')->setRoundingMode(\NumberFormatter::ROUND_CEILING);
setStoredAsString
By default, this field assumes that you store the value as a numeric property.
If you store the value as a string (e.g. because it's a very large number) use
this option to also display a <input type="text">
element instead of the
default <input type="number">
element:
1
yield NumberField::new('...')->setStoredAsString();
setThousandsSeparator
By default, the numeric value doesn't separate each thousands group in any way
(e.g. 12345.67
is displayed like that, instead of 12,345.67
). Use this option
to set the character to use to separate each thousands group:
1 2
// this would display '12345.67' as '12 345.67'
yield NumberField::new('...')->setThousandsSeparator(' ');