Currency Formatting!

I have an extended problem… I have searched through the entire forum to solve. I need to use .toLocaleString() on a repeater to add commas to the number set. I also need to add toFixed(2) in order to format that string with 2 decimal places so I can display it as currency.

any help will be amazing. still can’t believe there isn’t a simple currency ability in axure.

Currently I am using something like this.
$[[(item.column*1).toLocaleString()]]

this grabs 3456 and makes it $3,456 as it should

if I use
$[[(item.column*1).toFixed(2)]]

it grabs 3456 and makes it $3456.00 as expected

How do I combine the two so it’s a comma formatted number with 2 decimal places

The answer: not easily. Both those functions expect only numeric input and return a string that is not only numeric.

Will your inputs always be integers? If so, then you don’t need to do this and can fake it.

$[[Item.column.toLocaleString()]].00

P.S. You’re multiplying Item.column by 1, which does nothing other than throw an error if Item.column happens to not be a number. You can probably remove that, unless you have some other reason for doing so.

1 Like

Thanks for your reply

Data will come in as integers and decimals.

345.65 or 3456

thoughts?

Hi!

Where is regex when you need it?

This preposterous expression works even if the existing expression is already currency formatted. I put it on the OnLostFocus handler of the text field:

$[[ ((This.text.replace('$' , '').replace(',' , '')*1).toFixed(0)*1).toLocaleString() ]].[[(This.text.replace('$' , '').replace(',' , '')*1).toFixed(2).slice(-2)]]

Edit: oh: found a bug: if the source decimal place is .5 or over, you end up with a number one dollar too big. Use this instead.

$[[ Math.floor(This.text.replace('$' , '').replace(',' , '')).toLocaleString() ]].[[(This.text.replace('$' , '').replace(',' , '')*1).toFixed(2).slice(-2)]]

Test out the Live sample to make sure there are no bugs.

[Another edit] You can make this field less error prone if you restrict the characters that can be entered into it. This post will let you choose which specific keystrokes are allowed into the field. The list is long, since you’ll want the arrow keys, ctrl+c, etc., to all work, but I’ve done most of that for you. (You will need to add a comma and the dollar sign to the list so your text enterer can enter those.)

4 Likes

Can you provide the rp file? I would like to implement this but am not 100% sure how.

Hi Luxxer,

I responded to your question here:

1 Like

Hi! Sorry, just saw your message. Here is the file…
format-dollar.rp (47.1 KB)

2 Likes

2 posts were split to a new topic: Changing format for currency