Formats in React Numerictextbox component

10 Jan 20248 minutes to read

You can format the value of NumericTextBox using format property. The value will be displayed in the specified format when the component is in focused out state. The format string supports both the standard numeric format string and custom numeric format string.

Standard formats

From the standard numeric formats, you can use the numeric related format specifiers such as n,p and c in the NumericTextBox component. By using these format specifiers, you can achieve the percentage and currency textbox behavior also.

The below example demonstrates percentage and currency formats.

[Class-component]

import { NumericTextBoxComponent } from '@syncfusion/ej2-react-inputs';
import * as React from "react";
import * as ReactDOM from "react-dom";
// initializes NumericTextBox component
// sets percentage with 2 numbers of decimal places format
ReactDOM.render(<NumericTextBoxComponent format='p2' value={0.5} min={0} max={1} step={0.01} placeholder='Percentage format' floatLabelType='Auto'/>, document.getElementById('numericContainer1'));
// initializes NumericTextBox component
// sets currency with 2 numbers of decimal places format
ReactDOM.render(<NumericTextBoxComponent format='c2' value={10} placeholder='Currency format' floatLabelType='Auto'/>, document.getElementById('numericContainer2'));
import { NumericTextBoxComponent } from '@syncfusion/ej2-react-inputs';
import * as React from "react";
import * as ReactDOM from "react-dom";

// initializes NumericTextBox component
// sets percentage with 2 numbers of decimal places format
ReactDOM.render(<NumericTextBoxComponent format='p2' value={0.5} min={0} max={1} step={0.01} placeholder='Percentage format' floatLabelType='Auto' />
,document.getElementById('numericContainer1'));

// initializes NumericTextBox component
// sets currency with 2 numbers of decimal places format
ReactDOM.render(<NumericTextBoxComponent format='c2' value={10} placeholder='Currency format' floatLabelType='Auto' />
,document.getElementById('numericContainer2'));

[Functional-component]

import { NumericTextBoxComponent } from '@syncfusion/ej2-react-inputs';
import * as React from "react";
import * as ReactDOM from "react-dom";
// initializes NumericTextBox component
// sets percentage with 2 numbers of decimal places format
function App1() {
    return (<NumericTextBoxComponent format="p2" value={0.5} min={0} max={1} step={0.01} placeholder="Percentage format" floatLabelType="Auto"/>);
}
ReactDOM.render(<App1 />, document.getElementById('numericContainer1'));
// initializes NumericTextBox component
// sets currency with 2 numbers of decimal places format
function App2() {
    return (<NumericTextBoxComponent format="c2" value={10} placeholder="Currency format" floatLabelType="Auto"/>);
}
ReactDOM.render(<App2 />, document.getElementById('numericContainer2'));
import { NumericTextBoxComponent } from '@syncfusion/ej2-react-inputs';
import * as React from "react";
import * as ReactDOM from "react-dom";

// initializes NumericTextBox component
// sets percentage with 2 numbers of decimal places format
function App1() {
  return (
    <NumericTextBoxComponent
      format="p2"
      value={0.5}
      min={0}
      max={1}
      step={0.01}
      placeholder="Percentage format"
      floatLabelType="Auto"
    />
  );
}
ReactDOM.render(<App1 />, document.getElementById('numericContainer1'));

// initializes NumericTextBox component
// sets currency with 2 numbers of decimal places format
function App2() {
  return (
    <NumericTextBoxComponent
      format="c2"
      value={10}
      placeholder="Currency format"
      floatLabelType="Auto"
    />
  );
}
ReactDOM.render(<App2 />, document.getElementById('numericContainer2'));

Custom formats

From the custom numeric format string, you can provide any custom format by combining one or more custom specifiers.

The below examples demonstrate format the value by using currency format string # and 0.

[Class-component]

import { NumericTextBoxComponent } from '@syncfusion/ej2-react-inputs';
import * as React from "react";
import * as ReactDOM from "react-dom";
// initializes NumericTextBox component
// sets the format using custom format string `#`
ReactDOM.render(<NumericTextBoxComponent format='###.##' value={10} placeholder='Custom format string #' floatLabelType='Auto'/>, document.getElementById('numericContainer1'));
// initializes NumericTextBox component
// sets the format using custom format string `0`
ReactDOM.render(<NumericTextBoxComponent format='000.00' value={10} placeholder='Custom format string 0' floatLabelType='Auto'/>, document.getElementById('numericContainer2'));
import { NumericTextBoxComponent } from '@syncfusion/ej2-react-inputs';
import * as React from "react";
import * as ReactDOM from "react-dom";

// initializes NumericTextBox component
// sets the format using custom format string `#`
ReactDOM.render(<NumericTextBoxComponent format='###.##' value={10} placeholder='Custom format string #' floatLabelType='Auto'  />, document.getElementById('numericContainer1'));

// initializes NumericTextBox component
// sets the format using custom format string `0`
ReactDOM.render(<NumericTextBoxComponent format='000.00' value={10} placeholder='Custom format string 0' floatLabelType='Auto' />, document.getElementById('numericContainer2'));

[Functional-component]

import { NumericTextBoxComponent } from '@syncfusion/ej2-react-inputs';
import * as React from "react";
import * as ReactDOM from "react-dom";
// initializes NumericTextBox component
// sets the format using custom format string `#`
function App1() {
    return (<NumericTextBoxComponent format="###.##" value={10} placeholder="Custom format string #" floatLabelType="Auto"/>);
}
ReactDOM.render(<App1 />, document.getElementById('numericContainer1'));
// initializes NumericTextBox component
// sets the format using custom format string `0`
function App2() {
    return (<NumericTextBoxComponent format="000.00" value={10} placeholder="Custom format string 0" floatLabelType="Auto"/>);
}
ReactDOM.render(<App2 />, document.getElementById('numericContainer2'));
import { NumericTextBoxComponent } from '@syncfusion/ej2-react-inputs';
import * as React from "react";
import * as ReactDOM from "react-dom";

// initializes NumericTextBox component
// sets the format using custom format string `#`
function App1() {
  return (
    <NumericTextBoxComponent
      format="###.##"
      value={10}
      placeholder="Custom format string #"
      floatLabelType="Auto"
    />
  );
}
ReactDOM.render(<App1 />, document.getElementById('numericContainer1'));

// initializes NumericTextBox component
// sets the format using custom format string `0`
function App2() {
  return (
    <NumericTextBoxComponent
      format="000.00"
      value={10}
      placeholder="Custom format string 0"
      floatLabelType="Auto"
    />
  );
}
ReactDOM.render(<App2 />, document.getElementById('numericContainer2'));