Hi all,
I recently had to solve a problem and the resulting expressions have general applicability.
I see lots of posts about rounding and currency conversion of floats in Power Automate. Maybe there'll be a feature for this but in the meantime, here are a couple of JSON expressions I use.
#1. Round any float to 2 decimals.
Here is the expression. Replace "number" with your number or variable:
div(float(first(split(string(add(float(first(split(string(mul(number,100)),'.'))),mul(2,float(concat('.',last(split(string(mul(number,100)),'.'))))))),'.'))),100)
Here is the logic:
#2 Convert any decimal of 2 decimals and less than 1 billion to currency format.
Here's the expression. Replace "number" with your number. This is useful in combination with #1 above.
if(or(equals(number,0),equals(number,null)),'$0.00',if(greater(length(string(mul(number,100))),8),concat('$',substring(string(mul(number,100)),0,sub(length(string(mul(number,100))),8)),',',substring(string(mul(number,100)),sub(length(string(mul(number,100))),8),3),',',substring(string(mul(number,100)),sub(length(string(mul(number,100))),5),3),'.',substring(string(mul(number,100)),sub(length(string(mul(number,100))),2),2)),if(greater(length(string(mul(number,100))),5),concat('$',substring(string(mul(number,100)),0,sub(length(string(mul(number,100))),5)),',',substring(string(mul(number,100)),sub(length(string(mul(number,100))),5),3),'.',substring(string(mul(number,100)),sub(length(string(mul(number,100))),2),2)),if(greater(length(string(mul(number,100))),2),concat('$',substring(string(mul(number,100)),0,sub(length(string(mul(number,100))),2)),'.',substring(string(mul(number,100)),sub(length(string(mul(number,100))),2),2)),if(greater(length(string(mul(number,100))),1),concat('$0.',string(mul(number,100))),concat('$0.0',string(mul(number,100))))))))
Here's the logic (very loosely):
Solved! Go to Solution.
Very handy, thanks! I will be copying your post below so that it can be accepted as a solution to help with SEO optimization for more views.
@jzcase wrote:
Hi all,
I recently had to solve a problem and the resulting expressions have general applicability.
I see lots of posts about rounding and currency conversion of floats in Power Automate. Maybe there'll be a feature for this but in the meantime, here are a couple of JSON expressions I use.
#1. Round any float to 2 decimals.
Here is the expression. Replace "number" with your number or variable:
div(float(first(split(string(add(float(first(split(string(mul(number,100)),'.'))),mul(2,float(concat('.',last(split(string(mul(number,100)),'.'))))))),'.'))),100)
Here is the logic:
- Multiply x 100
- Convert to string
- Split at the decimal
- Take what's left of the decimal (first()) and convert to float
- Take what's right of the decimal (last()) and convert to float
- Multiply step five x 2
- Add together steps four and six.
- Convert result to string
- Split the string at the decimal
- Take what's left of the decimal (first()).
- Convert to float
- Divide by 100
#2 Convert any decimal of 2 decimals and less than 1 billion to currency format.
Here's the expression. Replace "number" with your number. This is useful in combination with #1 above.
if(or(equals(number,0),equals(number,null)),'$0.00',if(greater(length(string(mul(number,100))),8),concat('$',substring(string(mul(number,100)),0,sub(length(string(mul(number,100))),8)),',',substring(string(mul(number,100)),sub(length(string(mul(number,100))),8),3),',',substring(string(mul(number,100)),sub(length(string(mul(number,100))),5),3),'.',substring(string(mul(number,100)),sub(length(string(mul(number,100))),2),2)),if(greater(length(string(mul(number,100))),5),concat('$',substring(string(mul(number,100)),0,sub(length(string(mul(number,100))),5)),',',substring(string(mul(number,100)),sub(length(string(mul(number,100))),5),3),'.',substring(string(mul(number,100)),sub(length(string(mul(number,100))),2),2)),if(greater(length(string(mul(number,100))),2),concat('$',substring(string(mul(number,100)),0,sub(length(string(mul(number,100))),2)),'.',substring(string(mul(number,100)),sub(length(string(mul(number,100))),2),2)),if(greater(length(string(mul(number,100))),1),concat('$0.',string(mul(number,100))),concat('$0.0',string(mul(number,100))))))))
Here's the logic (very loosely):
- Set 0 or null values to $0.00
- Otherwise multiply number x 100
- Convert to string
- Concatenate the following (for numbers >= 1 million):
- $
- substring of the string from 0 to the length of the string minus 8
- comma
- substring of the string from the last place to the length of the string minus 3
- comma
- substring of the string from the last place to the length of the string minus 3
- period
- substring of the string from the last place to the length of the string minus 2
- Do same kind of thing for numbers in the thousands and hundreds
- For values less than 1 but more than 0.09, concatenate "$0." and the pennies
- For values 0.09 or less, concatenate "$0.0" and the pennies
Very handy, thanks! I will be copying your post below so that it can be accepted as a solution to help with SEO optimization for more views.
@jzcase wrote:
Hi all,
I recently had to solve a problem and the resulting expressions have general applicability.
I see lots of posts about rounding and currency conversion of floats in Power Automate. Maybe there'll be a feature for this but in the meantime, here are a couple of JSON expressions I use.
#1. Round any float to 2 decimals.
Here is the expression. Replace "number" with your number or variable:
div(float(first(split(string(add(float(first(split(string(mul(number,100)),'.'))),mul(2,float(concat('.',last(split(string(mul(number,100)),'.'))))))),'.'))),100)
Here is the logic:
- Multiply x 100
- Convert to string
- Split at the decimal
- Take what's left of the decimal (first()) and convert to float
- Take what's right of the decimal (last()) and convert to float
- Multiply step five x 2
- Add together steps four and six.
- Convert result to string
- Split the string at the decimal
- Take what's left of the decimal (first()).
- Convert to float
- Divide by 100
#2 Convert any decimal of 2 decimals and less than 1 billion to currency format.
Here's the expression. Replace "number" with your number. This is useful in combination with #1 above.
if(or(equals(number,0),equals(number,null)),'$0.00',if(greater(length(string(mul(number,100))),8),concat('$',substring(string(mul(number,100)),0,sub(length(string(mul(number,100))),8)),',',substring(string(mul(number,100)),sub(length(string(mul(number,100))),8),3),',',substring(string(mul(number,100)),sub(length(string(mul(number,100))),5),3),'.',substring(string(mul(number,100)),sub(length(string(mul(number,100))),2),2)),if(greater(length(string(mul(number,100))),5),concat('$',substring(string(mul(number,100)),0,sub(length(string(mul(number,100))),5)),',',substring(string(mul(number,100)),sub(length(string(mul(number,100))),5),3),'.',substring(string(mul(number,100)),sub(length(string(mul(number,100))),2),2)),if(greater(length(string(mul(number,100))),2),concat('$',substring(string(mul(number,100)),0,sub(length(string(mul(number,100))),2)),'.',substring(string(mul(number,100)),sub(length(string(mul(number,100))),2),2)),if(greater(length(string(mul(number,100))),1),concat('$0.',string(mul(number,100))),concat('$0.0',string(mul(number,100))))))))
Here's the logic (very loosely):
- Set 0 or null values to $0.00
- Otherwise multiply number x 100
- Convert to string
- Concatenate the following (for numbers >= 1 million):
- $
- substring of the string from 0 to the length of the string minus 8
- comma
- substring of the string from the last place to the length of the string minus 3
- comma
- substring of the string from the last place to the length of the string minus 3
- period
- substring of the string from the last place to the length of the string minus 2
- Do same kind of thing for numbers in the thousands and hundreds
- For values less than 1 but more than 0.09, concatenate "$0." and the pennies
- For values 0.09 or less, concatenate "$0.0" and the pennies
Join digitally, March 2–4, 2021 to explore new tech that's ready to implement. Experience the keynote in mixed reality through AltspaceVR!
Power Platform release plan for the 2021 release wave 1 describes all new features releasing from April through September 2021.
User | Count |
---|---|
87 | |
52 | |
35 | |
32 | |
27 |
User | Count |
---|---|
76 | |
66 | |
50 | |
46 | |
42 |