Domanda

I'm new to knockout and just starting to get my head around the framework. However, I've come into a problem whereby I'm trying to format large numbers with commas. I've been able to get the number to format to decimal places using the extenders API but this isn't what I want.

The number is stored in an array and an example of a number used in the app will be 5 million. So I need the values to print out 5,000,000 - is this possible? I'm guessing it has to be.

È stato utile?

Soluzione

For the formatting, you can use the following regex (warning: it does not work with float): mystring.replace(/\B(?=(\d{3})+(?!\d))/g, ',')

You can check out this example: http://jsfiddle.net/nyothecat/XgezN/1/

Altri suggerimenti

I think you'll want to create a custom binding. The easiest thing would probably be to use an existing jQuery formatter to format the display when the observable changes and then set up an event handler for the textbox to parse the textbox (removing the commas) when the users types something new.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top