Skip to content

Latest commit

 

History

History
63 lines (44 loc) · 1.51 KB

formatUnit.md

File metadata and controls

63 lines (44 loc) · 1.51 KB

formatUnit

Formats a unit based on the locale and options.

formatUnit(value, unit, options?)

Arguments

  • value - number - Number to be formatted
  • unit - string - Unit to use (see below)
  • options? - object - Formatting options

The unit argument can be a unit of time (e.g. second, day, etc.), a unit of measurement (e.g. mile, meter, gigabyte), or a compound unit (e.g. mile-per-hour, kilowatt-hour).

Example

import { useGlobalize } from 'react-native-globalize';

const ExampleComponent = () => {
  const { formatUnit } = useGlobalize();

  formatUnit(50, 'mile-per-hour');
  // 50 miles per hour

  formatUNit(50, 'kilometer-per-hour');
  // 50 kilometers per hour
};

Options

form

Type Required Default Description
string No none Use alternate display format. Possible values: short, narrow.
formatUnit(50, 'mile-per-hour', { form: 'short' });
// 50 mph

numberFormatter

Type Required Default Description
function No getNumberFormatter() Customize the number formatting function.
const { getNumberFormatter } = useGlobalize();

formatUnit(5000, 'gigabyte', {
  numberFormatter: getNumberFormatter({
    minimumFractionDigits: 2,
    useGrouping: false,
  }),
});
// 5000.00 gigabytes