Thursday 4 February 2010

InfoPath, How to play with decimal rounding

Background:
My client had asked me to provide the rounding in a browser enabled form. The rounding requirements were bit quirky to what we deal in day to day basis. Here's the requirement. Quirky isn't it?


Mid Point Rounding:
7.234 should be rounded off to 7.5 same for the others between 7.0 to 7.5.
Ceiling:
7.6 should be rounded off to 8.0 same for the others between 7.5 to 8.0


The form template is browser enabled.


Workaround:
I played with the following bits in order to get this thing working.



  • Field Rules
  • Rules Conditions
  • Math Functions
  • Set field value actions



Here's the cut down implementation


1-Change the field type to decimal and set the format to one decimal place


2-Add rule for 'Mid Point'

3-Set condition with the following clauses
desired field value 'is greater than' floor(desired field value)
and
desired field value 'is less than' floor(desired field value) + 0.5

4-Set the value


5- Add rule for ceiling value



6-Set condition with the following clauses
desired field value 'is greater than' floor(desired field value) + 0.5
and
desired field value 'is less than' floor(desired field value) + 1

7-Set the value


and finally the completed rules look like this


These rules round the 7.6 to 8.0 and 7.1 to 7.5...

The whole purpose behind this post is to give the idea how the complex rounding can be achieved in InfoPath using rules and without writing the managed code.

If you have more complex requirements, I'm sure you could implement this way or the other. There's a variety of math functions available in InfoPath. You can find the complete reference here..



Hope this will help !

Majid

No comments:

Iconise ‘Add Picture’ control in PowerApps

Default look you get for ‘Add Picture’ control is not very appealing for end user and there is no OTB way of specifying icon for this con...