cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
jzcase
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
Brad_Groux
Community Champion
Community Champion

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

2 REPLIES 2
Brad_Groux
Community Champion
Community Champion

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

leyburn19
Memorable Member
Memorable Member

I would have gone bonkers trying to achieve that - Awesome

Helpful resources

Announcements
MPA_User Group Leader_768x460.jpg

Manage your user group events

Check out the News & Announcements to learn more.

V3_PVA CAmpaign Carousel.png

Community Challenge - Giveaways!

Participate in the Power Virtual Agents Community Challenge

Carousel 2021 Release Wave 2 Plan 768x460.jpg

2021 Release Wave 2 Plan

Power Platform release plan for the 2021 release wave 2 describes all new features releasing from October 2021 through March 2022.

PowerPlatform 768x460.png

Microsoft Learn

Check out our new Discover Your Career Path blog post series and get all the details.

Top Solution Authors
Users online (2,389)