question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to use currency format to fill decimal/cents first?

See original GitHub issue

Consider the following input value: 12345

The output would be: $ 123.45 instead of $ 1,2345.00.

How to achieve the first output using this library?

This is how I am using right now, unfortunately I couldn’t make it work as desired:

<NumberFormat
  customInput={Input}
  decimalScale={2}
  decimalSeparator=","
  fixedDecimalScale
  onKeyDown={onEnter(handleEnter)}
  onValueChange={handleValueChange}
  placeholder="$ 0,00"
  prefix="$ "
  thousandSeparator="."
  value={value}
/>

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:8
  • Comments:16 (1 by maintainers)

github_iconTop GitHub Comments

96reactions
richardaumcommented, Nov 9, 2019

@felri I’ve found out how to format the number properly - using a formatter. The trick is to divide value by 100.

export default function currencyFormatter(value) {
  if (!Number(value)) return "";

  const amount = new Intl.NumberFormat("pt-BR", {
    style: "currency",
    currency: "BRL"
  }).format(value / 100);

  return `${amount}`;
}

Then you must use this format into your component:

<NumberFormat {...allOtherRequiredProps} format={currencyFormatter} />
5reactions
richardaumcommented, Jun 14, 2020

Yeah. This is expected because the approach is based on a formatter only. So it will only change how the number is displayed. You must handle the float number and divide it by 100 before persisting it or whatever you do with this number.

An alternative could be if a implementation is made to make this feature native, then we could expect float number also be divide by 100 automatically.

You could also implement your own wrapper to this component and expose a float number already divided by 100.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How do you format a number to currency when using React ...
You can use toFixed method for showing 2 decimal point. let num = 1000; console.log(num.toFixed(2)); // 1000.00. And you can use Regex like ......
Read more >
Format numbers as currency - Microsoft Support
In the Decimal places box, enter the number of decimal places that you want for the number. For example, to display $138,691 instead...
Read more >
React js Number Format and styling | by Nidhin kumar
style — The formatting style to use. Possible values are "decimal" for plain number formatting, "currency" for currency formatting, ...
Read more >
Custom Excel number format - Ablebits
If a custom number format includes 2 sections, the first section is used ... after a number to fill the cell, use this...
Read more >
International Currency Conversion - DFA Cornell
United States (U.S.) currency is formatted with a decimal point (.) as a separator between the dollars and cents. Some countries use a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found