cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Damo_R
Level: Powered On

Format decimal numbers

I am trying to format the data of a calculated field within the flow to send in an email to show 2 decimal places. has anyone acheived this? 

 

I have tried both Text and Round and several iterations of these functions and they come back as invalid templates. 

 

Can anyone shine any light on this? 

 

Capture.JPG

 

the value from the column is 1.50000000000 i only want to see 1.5

1 ACCEPTED SOLUTION

Accepted Solutions
Super User
Super User

Re: Format decimal numbers

As far as I know, math functions currently supported in Microsoft FLow as "expressions" are the ones defined here:

https://docs.microsoft.com/en-us/azure/logic-apps/logic-apps-workflow-definition-language

 

I guess you can multiply by 100, convert into integer, get back to float and divide into 100; but I haven't tested myself; so unsure if both float() and int() accepts non-string parameters

 

Hope this helps

 





If anybody solved your question, remember to mark its post as a solution!

Proud to be a Flownaut!





9 REPLIES 9
Super User
Super User

Re: Format decimal numbers

As far as I know, math functions currently supported in Microsoft FLow as "expressions" are the ones defined here:

https://docs.microsoft.com/en-us/azure/logic-apps/logic-apps-workflow-definition-language

 

I guess you can multiply by 100, convert into integer, get back to float and divide into 100; but I haven't tested myself; so unsure if both float() and int() accepts non-string parameters

 

Hope this helps

 





If anybody solved your question, remember to mark its post as a solution!

Proud to be a Flownaut!





Damo_R
Level: Powered On

Re: Format decimal numbers

@efialttes thank you so much for your repsponse and the link you provided. With a little reading and some trial and error i have come up with the following:

 

if(or(greater(body('Create_item')?['Days_x0020_Total'], string (10)),equals(body('Create_item')?['Days_x0020_Total'],string(10))),take(body('Create_item')?['Days_x0020_Total'],4),take(body('Create_item')?['Days_x0020_Total'],3))

 

This basically looks at the value in the total days field and determines if it is greater or equal to 10, if yes then it will produce 4 place and if no it will produce 3 places.

 

works a treat

Super User
Super User

Re: Format decimal numbers

@Damo_R

You are welcome. Thanks for sharing!





If anybody solved your question, remember to mark its post as a solution!

Proud to be a Flownaut!





StevenWade
Level: Powered On

Rounding for specific scenarios

There's still no generalized rounding solution in Flow, which means building solutions specific to your data. Here's an approach I took when I needed to round from three decimals (#.###) to two (#.##) for display purposes. Fortunately, I knew I'd always be getting data in a #.### format, but you could pretty easily extend this to find the decimal place if the number of places to the left of the decimal was unknown.

 

if(contains('56789', substring(string(triggerBody()['number']),4,1)),substring(string(add(triggerBody()['number'],0.01)),0,4),substring(string(triggerBody()['number']),0,4))

henrim
Level: Powered On

Re: Format decimal numbers

I just used your suggestion to round the result of my division formula:

Original expression:

div(variables('Sum'),5)

Which returned the full result, i.e 1.6666666 or similar.

 

I formatted it to 1 decimal using this expression:

div(float(int(mul(div(variables('Sum'),5),10))),10)

 

that is

  • take result from division
  • multiple by 10
  • converting to int
  • converting to float (else it became an integer result)
  • dividing by 10

 

Thanks for the hint!

jan-dolejsi
Level: Powered On

Re: Format decimal numbers

It was not as simple, because int() and float() only accept string arguments. So you get a run-time error like this:

 

InvalidTemplate. Unable to process template language expressions in action 'Rounded' inputs at line '1' and column '2484': 'The template language function 'int' was invoked with a parameter that is not valid. The value cannot be converted to the target type.'.
 
On top of that, the  int() function only accepts string representations of integer numbers - it does not accept any floating point number. So I had to turn the number to string, select the part before the '.' and pass it to  int(). Not sure if the '.' is subject to localization, which would obviously fail. 
 
This is the expression I've put into a Compose action:
div(int(first(split(string(mul(123.456, 10.0)),'.'))), 10.0)
The output was 
123.4
aCTim
Level: Power Up

Re: Format decimal numbers

Thanks to Damo_R I was able to round numbers for our case, with two numbers behind. A little bit ugly, but it works.

 

Initializing 2 variables.

variable var as string 

first(split(string(mul(123.454, 1000.0)),'.'))

(you can put your number or varible where 123.454 is)

variable varTemp as Integer

int(variables('var'))

than we go on with a condition

if

int(last(variables('var')))

equal or even to 5

 

set var to

sub(add(int(variables('varTemp')),10),int(last(string(variables('varTemp')))))

else

set var to 

sub(int(variables('varTemp')),int(last(string(variables('varTemp')))))

finally transform it to a float (int would slice the numer)

div(float(int(div(int(variables('var')),10))),100)

 

Damo_R
Level: Powered On

Re: Format decimal numbers

Glad my efforts could help you
Highlighted
degvalentine
Level: Powered On

Re: Format decimal numbers

Here's a similar solution, but no temp variables are needed.

Uses variables('myFloat') to represent the number you want to round.

div(
add(
mul(variables('myFloat'), 1000),
sub(
if(
greaterOrEquals(
mod(mul(variables('myFloat'), 1000), 10),
5
),
10,
0
),
mod(mul(variables('myFloat'), 1000), 10)
)
),
1000
)

Or, all in one line: 

div(add(mul(variables('myFloat'),1000),sub(if(greaterOrEquals(mod(mul(variables('myFloat'),1000),10),5),10,0),mod(mul(variables('myFloat'),1000),10))),1000)

The float is multilpiled by 1000 before doing modulo to avoid tiny-fraction math errors (e.g. returning 0.00499999... instead of 0.005).

 

Using add, sub, mul, div, and mod to round a float... a reasonable workaround... XD

 

If you're looking for an expression to format for currency with a thousands separator, see my expression in another post.

Helpful resources

Announcements
firstImage

Microsoft Flow Online Conference

Join us for a FULL day of FREE Microsoft Flow Sessions from some of the best minds in the industry!

firstImage

Watch Sessions On Demand!

Continue your learning in our online communities.

Power Platform 2019 Release Wave 2 Plan

Power Platform 2019 Release Wave 2 Plan

Features releasing from October 2019 through March 2020

thirdimage

Flow Community User Group Member Badge

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

thirdImage

New Flow Community Board!

Check out the new Microsoft Flow Community Blog Topic Suggestion board!

fifthimage

Microsoft Learn

Learn how to build the business apps that you need

sixthImage

Power Platform World Tour

Find out where you can attend!

seventhimage

Webinars & Video Gallery

Watch & learn from the Flow Community Video Gallery!

Top Kudoed Authors
Users Online
Currently online: 40 members 4,412 guests
Please welcome our newest community members: