Working with Money and Number Formats
Learn how to work with various types of money input and output formats, using Corvid (a.k.a. Wix Code). Learn how to convert money formats to number formats and visa versa.
This example shows you how to allow the user to input any type of money format regardless of dollar sign, commas or decimals and converts it into a number format for calculation, display or saving to a collection. This example also shows you how to take a number format and convert it back into a money format including dollar sign and comma separator for thousands and millions of dollars. Try the example below.
Enter an amount to start!
Enter an number to start!
Add an onKeyPress event listener for #iptMoney and #iptNumber to listen for user entry and convert it from money to number or number to money. Both event listeners have a one second delay to allow the user to finish typing.
The formatMoney function converts money amounts such as $10000, 10,000 or $10,000 into 10000, as displayed in #txtNumber. The formatNumber function converts number amounts such as 10000 or 100 to $10,000 and $100 respectively, as displayed in #txtMoney. It relies on an additional function which uses regular expressions to create a format for use on numbers. The isNaN function is used to test whether the value is a number or not, and stands for 'is not a number'.