Round number and place thousands seperator

  • tyekynan
  • Topic Author
  • Offline
  • Senior Boarder
  • Senior Boarder
  • Posts: 50
  • Thank you received: 2

tyekynan created the topic: Round number and place thousands seperator

I have several calculated fields that are dollar amounts.

I would like for these values to show up with a thousands separator.

Current: $10000, $1000
Need: $10,000, $1,000

Also, sometimes the fields show up like this: $1000.00003
I would like to round up or down to the nearest dollar amount.
$1000.0003 would be $1000
$1000.58023 would be $1001

I have seen some other posts about thousands separates but I am not sure where to put some of the code, as the guys that posted the initial question seemed to be more in tune than I.
#234544
  • Posts: 2282
  • Karma: 84
  • Thank you received: 306

mihaela replied the topic: Round number and place thousands seperator

Hello,

Here is the code that will make the changes that you want.
  // get that field's value
  var tmp = ff_getElementByName('elementname').value;

  //first remove dollar sign
  tmp = tmp.replace(/\$/g, '');

  // round the number
  tmp = Math.round(Number(tmp));

  // add thousands separator
  tmp = tmp.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
  
  // add dollar sign back 
  tmp = '$' + tmp;

  // assign value to variable
  ff_getElementByName('elementname').value = tmp;
NOTE: In order for this code to work you have to replace elementname with the Name of the element that will display that number.

Since you said this is a calculated amount you have to put this code to the end of calculation.
Also, since I assume that you are in your code adding dollar sign, you can skip the adding and removing and just add dollar sign when you are done with adjusting the number.

Let me know whether you have managed to make this work for your form as you wanted.
If you have more questions, feel free to ask.

Regards,

Mihaela
#234547
  • tyekynan
  • Topic Author
  • Offline
  • Senior Boarder
  • Senior Boarder
  • Posts: 50
  • Thank you received: 2

tyekynan replied the topic: Round number and place thousands seperator

So here is the actionscript of one of my fields. How would I go about adding the above code to it?

function ff_point_balance_action(element, action)
{
ff_getElementByName('point_balance').value = "$" +
(Number(ff_getElementByName('points_field').value) *
Number(ff_getElementByName('ppp_field').value));
}
// ff_point_balance_action
#234549
  • Posts: 2282
  • Karma: 84
  • Thank you received: 306

mihaela replied the topic: Round number and place thousands seperator

Hello,

The whole code in this case would look like the following:
function ff_point_balance_action(element, action)
{
  // get that field's value
  var tmp = Number(ff_getElementByName('points_field').value) *
Number(ff_getElementByName('ppp_field').value)

  // round the number
  tmp = Math.round(Number(tmp));

  // add thousands separator
  tmp = tmp.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
  
  // add dollar sign back 
  tmp = '$' + tmp;

  // assign value to variable
  ff_getElementByName('point_balance').value = tmp;
}
// ff_point_balance_action

Let me know whether it worked.

Regards,

Mihaela
#234561
Moderators: ForumSupporttomeperica
Time to create page: 0.103 seconds

Community Reward

Help us to create new extensions and plugins! With only $5 you help us a lot and get unlimited download access to all of our products, professional support and even more. Get your reward now!

Read More Here

News and Updates

Get informed about new downloads, updates and more in our News and Updates newsletter.

All Extensions Subscription

Get 1 year access to all of our current and future products and 1 year of professional support -- 99€

No support per domain or website installation limits! Includes all of our current and future Joomla!® extensions, Joomla!® templates for the duration of your membership. This means, by purchasing an All Extensions Subscription you'll have it all covered!

Get it from here

3rd Party Discount - 25% Off

We help you to keep your costs under control. If you are a new member and purchased a form building tool from a different form vendor, then you'll get a 25% discount on our subscription plans.

How to receive the discount:

Send us a quick email to sales@crosstec.org with a proof of purchase (for example a paypal receipt), await payment instructions and enjoy your membership!

Community Reward

Help us to create new extensions and plugins! With only $5 you help us a lot and get unlimited download access to all of our products, professional support and even more. Get your reward now!

Read More Here