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

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

leyburn19
Memorable Member
Memorable Member

I would have gone bonkers trying to achieve that - Awesome

Helpful resources

Announcements
Power Platform Conf 2022 768x460.jpg

Join us for Microsoft Power Platform Conference

The first Microsoft-sponsored Power Platform Conference is coming in September. 100+ speakers, 150+ sessions, and what's new and next for Power Platform.

New Ideas Forum MPA.jpg

A new place to submit your Ideas for Power Automate

Announcing a new way to share your feedback with the Power Automate Team.

MPA Virtual Workshop Carousel 768x460.png

Register for a Free Workshop

Learn to digitize and optimize business processes and connect all your applications to share data in real time.

MPA Licensing.jpg

Ask your licensing questions at the Power Automate AMA!

Join Priya Kodukula and the licensing team, super users and MVPs to find answers to your questions on Power Automate licensing.

Users online (1,644)