Using Javascript to format numbers

 6 Replies
 0 Subscribed to this topic
 52 Subscribed to this forum
Sort:
Author
Messages
Mike LaCosse
Basic Member Send Private Message
Posts: 6
Basic Member

We are using ProcessFlow with an 8.0 environment, meaning all of our emails are plain text.  To assist with formatting so we can make the emails look a little better, I'm trying to get a "Double" variable formatted to display 2 decimal places.  This is easy to do using the round(var,2) function, but if the variable is an integer it won't display the .00.  So we'll see "16" instead of "16.00", but if the variable is 16.49 that will display correctly.

Has anyone used a javascript function to force a double variable to display 2 decimal places?

Thanks

John Henley
Send Private Message
Posts: 3351
Try using the .toFixed(2) method on your variable, e.g. variablename.toFixed(2)
Thanks for using the LawsonGuru.com forums!
John
Mike LaCosse
Basic Member Send Private Message
Posts: 6
Basic Member

toFixed is working to convert longer decimals into 2 decimals, but it still isn't showing trailing zeroes. 

Double WHOLE = 16

Double DECIMAL = 16.4801

WHOLE.toFixed(2) returns "16", and DECIMAL.toFixed(2) returns 16.48.  I was thinking this was not a problem with the toFixed method, but with the way ProcessFlow carries and displays numeric objects. 

Shane Jones
Veteran Member Send Private Message
Posts: 460
Veteran Member
I must have gotten booted out (timed out)... This might work with some tinkering...

I wrote a suggestion for this that was quite complete but I will summarize the idea again. Change the number to a string "toString()" then look for the decimal location. Use inStr(variable, ".") Add one or two for the value in the first and second location following the decimal charAt(instrvalue+1) or charAt(instrvalue+2). If 1st is null then add ".00" to the end of the string number. If it is 0 then look at the 2nd position because you might need to add a "0" for the second position after the decimal.

Hope this gives you some ideas...
Shane Jones
Tools: HR, Payroll, Benefits, PFI, Smart Office, BSI, Portal and Self-Service
Systems: Lawson, Open Hire, Kronos, Crystal Reporting, SumTotal Learning
** Teach others to fish...
Mike LaCosse
Basic Member Send Private Message
Posts: 6
Basic Member

Thanks Shane. 

I was hoping to do this without all the extra branch nodes, but I ended up using them anyway.  The node checks if it's a whole number using PRICE == round(PRICE, 0), and if it is a whole number it converts the number to a string (as you suggested) and adds .00 to the end. 

John Henley
Send Private Message
Posts: 3351
Mike, you don't need the branch nodes, you can put multiple lines of Javascript in the Assign node.
Thanks for using the LawsonGuru.com forums!
John
Brian Espe
Basic Member Send Private Message
Posts: 7
Basic Member
Shane,  We are new to process flow and javascript.  Would you be willing to share the syntax for this resolution.  We have been struggling on trying to get the trailing zeroes to work on our amount fields.

Brian Espe
Unified Grocers.