Formatting numbers in JavaScript

Viewing 1 reply thread
  • Author
    Posts
  • March 27, 2021 at 12:00 PM #43943

    Victor Warner
    Participant

    For Brendan,

    I have asked this question before and have received an answers, but until now not needed it. However I can see I have asked 49 questions but cannot access any posting beyond the first page. It is likely the answer is on one of the pages I cannot access.

    Now the question…

    Question

    In some of the scripts I have created there are figures / sums taken from fields.

    But I would like to format them correctly. For example:

    1. if the amount is 150.00 it appears in the script is run as 150
    2. if the amount is 150.50 it appears in the script as 150.5

    etc.

    How is it possible to format numbers so that there are always two decimal places?

    March 27, 2021 at 5:25 PM #43947

    Sam Moffatt
    Participant

    Sometimes you can use Google to search the site (e.g. https://www.google.com/search?client=safari&rls=en&q=site:www.tapforms.com/forums+number+formatting&ie=UTF-8&oe=UTF-8) to find stuff.

    If you have a simple return value in your script field (e.g. you return 150 or 150.5) if you set the format of the field to number, you can use the Tap Forms field formatting options to change the number of decimal places to be two.

    If you need to format in Javascript, I made a port of a Javascript sprintf implementation as a part of the Script Manager stuff or you can directly grab it from GitHub. This adds an sprintf function you can use to format numbers in Javascript as a part of a larger string formatter.

Viewing 1 reply thread

You must be logged in to reply to this topic.