Tap Forms – Organizer Database App for Mac, iPhone, and iPad › Forums › Script Talk › Making a number show 2 decimal places in a script
- This topic has 3 replies, 3 voices, and was last updated 4 months, 2 weeks ago by JScottA.
-
AuthorPosts
-
July 31, 2024 at 4:22 PM #50967
Victor WarnerParticipantI have a script where several fields are concatenated together including one which contains a number:
function Third_Party_Charges_Csv() { // Fetch content from table var third_party_charges = record.getFieldValue('fld-021cfb3cc4f446ddbf5f9463c7b5d620'); // Define field ids from table columns const item_id = 'fld-410bdf5d624a4651b8a803becd9c6578'; const cost_per_item_id = 'fld-6cea4aa73ad44b6f91ba7f566e321790'; const number_id = 'fld-95791fbc24bd4f3ca714756e5dd6a060'; const cost_id = 'fld-a0be1b009c6a4a99a1784d36da134ee3'; var txt = []; // Loop over all entries and fetch field content. for (var n = 0; n < third_party_charges.length; n++){ var third_party_charge = third_party_charges[n]; var item = third_party_charge.getFieldValue(item_id); var cost_per_item = third_party_charge.getFieldValue(cost_per_item_id); var number = third_party_charge.getFieldValue(number_id); var cost = third_party_charge.getFieldValue(cost_id); // Format field content as desired txt.push((n+1) + ". " + item + ": £" + cost_per_item + " x " + number + " = £" + cost); } // Combine each formatted table row return txt.join(" - "); } Third_Party_Charges_Csv();
In the field it displays (as currency) as, eg 32.40, but in the script it shows as 32.4.
Is there a way to make the script show numbers with two decimal places?
August 1, 2024 at 10:04 AM #50972
BrendanKeymastercost_per_item.toFixed(2)
should do it.August 2, 2024 at 4:47 AM #50973
Victor WarnerParticipantBrendan,
Thank you for the answer.
Victor
August 3, 2024 at 9:16 AM #50976
JScottAParticipantFYI…pasted Vitor’s original post into the assistant GPT. For the record, I liked Brendan’s very concise and to the point response better, for my typical requests. Anyway, here is the complete GPT response:
To display numbers with two decimal places in your Tap Forms script, you can use JavaScript’s
toFixed()
method. Here is your script modified to ensure that numbers are formatted to always show two decimal places:`javascript
function Third_Party_Charges_Csv() {// Fetch content from table
var third_party_charges = record.getFieldValue(‘fld-021cfb3cc4f446ddbf5f9463c7b5d620’);// Define field ids from table columns
const item_id = ‘fld-410bdf5d624a4651b8a803becd9c6578’;
const cost_per_item_id = ‘fld-6cea4aa73ad44b6f91ba7f566e321790’;
const number_id = ‘fld-95791fbc24bd4f3ca714756e5dd6a060’;
const cost_id = ‘fld-a0be1b009c6a4a99a1784d36da134ee3’;var txt = [];
// Loop over all entries and fetch field content.
for (var n = 0; n < third_party_charges.length; n++){
var third_party_charge = third_party_charges[n];
var item = third_party_charge.getFieldValue(item_id);
var cost_per_item = parseFloat(third_party_charge.getFieldValue(cost_per_item_id)).toFixed(2);
var number = parseFloat(third_party_charge.getFieldValue(number_id)).toFixed(2);
var cost = parseFloat(third_party_charge.getFieldValue(cost_id)).toFixed(2);// Format field content as desired
txt.push((n+1) + “. ” + item + “: £” + cost_per_item + ” x ” + number + ” = £” + cost);
}// Combine each formatted table row
return txt.join(” – “);
}Third_Party_Charges_Csv();
`
By using
parseFloat()
andtoFixed(2)
, this script ensures that thecost_per_item
,number
, andcost
fields are always displayed with two decimal places [oai_citation:1,javascript – Format number to always show 2 decimal places – Stack Overflow](https://stackoverflow.com/questions/6134039/format-number-to-always-show-2-decimal-places) [oai_citation:2,javascript – How to format numbers? – Stack Overflow](https://stackoverflow.com/questions/5731193/how-to-format-numbers). -
AuthorPosts
You must be logged in to reply to this topic.