cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Resolver II
Resolver II

JSON expressions for rounding and currency conversion

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:

  1. Multiply x 100
  2. Convert to string
  3. Split at the decimal
  4. Take what's left of the decimal (first()) and convert to float
  5. Take what's right of the decimal (last()) and convert to float
  6. Multiply step five x 2
  7. Add together steps four and six.
  8. Convert result to string
  9. Split the string at the decimal
  10. Take what's left of the decimal (first()).
  11. Convert to float
  12. 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):

  1. Set 0 or null values to $0.00
  2. Otherwise multiply number x 100
  3. Convert to string
  4. Concatenate the following (for numbers >= 1 million):
    1. $
    2. substring of the string from 0 to the length of the string minus 8 
    3. comma
    4. substring of the string from the last place to the length of the string minus 3
    5. comma
    6. substring of the string from the last place to the length of the string minus 3
    7. period
    8. substring of the string from the last place to the length of the string minus 2
  5. Do same kind of thing for numbers in the thousands and hundreds
  6. For values less than 1 but more than 0.09, concatenate "$0." and the pennies
  7. For values 0.09 or less, concatenate "$0.0" and the pennies
1 ACCEPTED SOLUTION

Accepted Solutions
Super User II
Super User II

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:

  1. Multiply x 100
  2. Convert to string
  3. Split at the decimal
  4. Take what's left of the decimal (first()) and convert to float
  5. Take what's right of the decimal (last()) and convert to float
  6. Multiply step five x 2
  7. Add together steps four and six.
  8. Convert result to string
  9. Split the string at the decimal
  10. Take what's left of the decimal (first()).
  11. Convert to float
  12. 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):

  1. Set 0 or null values to $0.00
  2. Otherwise multiply number x 100
  3. Convert to string
  4. Concatenate the following (for numbers >= 1 million):
    1. $
    2. substring of the string from 0 to the length of the string minus 8 
    3. comma
    4. substring of the string from the last place to the length of the string minus 3
    5. comma
    6. substring of the string from the last place to the length of the string minus 3
    7. period
    8. substring of the string from the last place to the length of the string minus 2
  5. Do same kind of thing for numbers in the thousands and hundreds
  6. For values less than 1 but more than 0.09, concatenate "$0." and the pennies
  7. For values 0.09 or less, concatenate "$0.0" and the pennies

View solution in original post

1 REPLY 1
Super User II
Super User II

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:

  1. Multiply x 100
  2. Convert to string
  3. Split at the decimal
  4. Take what's left of the decimal (first()) and convert to float
  5. Take what's right of the decimal (last()) and convert to float
  6. Multiply step five x 2
  7. Add together steps four and six.
  8. Convert result to string
  9. Split the string at the decimal
  10. Take what's left of the decimal (first()).
  11. Convert to float
  12. 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):

  1. Set 0 or null values to $0.00
  2. Otherwise multiply number x 100
  3. Convert to string
  4. Concatenate the following (for numbers >= 1 million):
    1. $
    2. substring of the string from 0 to the length of the string minus 8 
    3. comma
    4. substring of the string from the last place to the length of the string minus 3
    5. comma
    6. substring of the string from the last place to the length of the string minus 3
    7. period
    8. substring of the string from the last place to the length of the string minus 2
  5. Do same kind of thing for numbers in the thousands and hundreds
  6. For values less than 1 but more than 0.09, concatenate "$0." and the pennies
  7. For values 0.09 or less, concatenate "$0.0" and the pennies

View solution in original post

Helpful resources

Announcements
Community Conference

Power Platform Community Conference

Check out the on demand sessions that are available now!

MPA Community Blog

Power Automate Community Blog

Check out the community blog page where you can find valuable learning material from community and product team members!

Top Solution Authors
Top Kudoed Authors
Users online (9,311)