cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Highlighted
jzcase
Level 8

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
Super User

Re: JSON expressions for rounding and currency conversion

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
Super User

Re: JSON expressions for rounding and currency conversion

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
firstImage

New Ranks and Rank Icons in April

Read the announcement for more information!

firstImage

Better Together Contest Finalists Announced!

Congrats to the finalists of our ‘Better Together’-themed T-shirt design contest! Click for the top entries.

firstImage

New & Improved Power Automate Community Cookbook

We've updated and improved the layout and uploading format of the Power Automate Cookbook!

thirdimage

Power Automate Community User Group Member Badge

Fill out a quick form to claim your user group badge now!

sixthImage

Community Summit North America

The top training and networking event across the globe for Microsoft Business Applications

Top Solution Authors
Top Kudoed Authors
Users online (5,022)