Monday, October 24, 2011

format-number with 0 in xslt using dot net

I was using the following code in my xslt to format number and then used to apply this xslt on xml in dot net.
<xsl:value-of select="format-number(number,'#.#####')" />
And it was showing result as required but when number value become 0 , it returns the empty as I tried to hard code number value with 0.
<xsl:value-of select="format-number(0,'#.#####')" />
and it was giving outuput 0 but  when I used the same code in simple xslt and simple xml (and linked xslt with xml file in note pad ) and runs this in browser , It gave me the correct result in form of 0.
I was surprised why this is not working in dot net code and working in browser with simple xslt file and xml file without using dot net code.

Then I found a solution regarding dot net as
<xsl:value-of select="format-number(0,'#0.#####')" />
But still I am not be able to understand why this is working in simple browser and not working in dot net code?


Sunday, October 23, 2011

Round issue in c#

Today one of my friend asked me that he has some problems while rounding value to 2 decimal places and he was trying to round "2.145" up to 2 decimal places , he used Math.Round() funciton but it was giving output as 2.14 then I told him to use Math.Round(value, 2, MidpointRounding.AwayFromZero)) and he used it and was very happy that his issue solved .
But later I came to know that there are still issues present with
Math.Round(value, 2, MidpointRounding.AwayFromZero)) as mentioned on msdn at
http://msdn.microsoft.com/en-us/library/f5898377.aspx
that it will round 0.145 to 0.14 instead of 0.15 and rounding 2.135 to 2.13 instead of 2.14 becuae of loss of precision.
I found the two solutions for the above problem

Solution 1:
Convert double to decimal
Math.Round((decimal(2.135), 2, MidpointRounding.AwayFromZero) it would show 2.14 and
Math.Round((decimal)0.145, 2, MidpointRounding.AwayFromZero) it would show 0.15

Solution 2:
use Math.Ceiling
System.Math.Ceiling(2.135 * 100) / 100 it would show 2.14
And
System.Math.Ceiling(0.145 * 100) / 100 it would show 0.15