Round up amount

Hi,

Below is function used to round up amount.

Function has two parameters : one is amount & second is amount round up digits.



Private Function RoundUpAmount(ByVal number As Double, ByVal digits As Integer) As Double

Dim Temp As String
Dim Temp2 As String
Dim i As Integer
Dim j As Integer
Dim ResultValue As Double
Dim Numbertemp As Double

Temp = Convert.ToString(number)
i = Temp.LastIndexOf(".")
If (((Temp.Length - (i + 1)) <= digits) Or (i = -1)) Then Return number
Temp2 = Temp.Substring(i + digits + 1, 1)
j = Convert.ToInt32(Temp2)
Numbertemp = Convert.ToDouble(Temp.Substring(0, i + digits + 1))

If j = 5 Then
ResultValue = Numbertemp + (1 / (Math.Pow(10, digits)))
Else
ResultValue = Math.Round(number, digits)
End If
Return ResultValue
End Function






e.g If i/p is
1. Response.Write(RoundUpAmount(133364.85566, 0))then o/p will be 133365
2. Response.Write(RoundUpAmount(133364.85566, 1))then o/p will be 133364.9
3. Response.Write(RoundUpAmount(133364.85566, 2))then o/p will be 133364.86


Comments

Author: Phagu Mahato25 Feb 2014 Member Level: Gold   Points : 6

Try this


using System;

public class Example
{
public static void Main()
{
double[] values = { 2.125, 2.135, 2.145, 3.125, 3.135, 3.145 };
foreach (double value in values)
Console.WriteLine("{0} --> {1}", value, Math.Round(value, 2));

}
}
Or

Public Function RoundUD(ByVal numD As Decimal, _
ByVal places As Integer, _
ByVal UorD As Boolean) As Decimal

'UorD - True = up, false = down
Dim dr As Decimal
Try
Dim r As Decimal = CDec(10 ^ (places + 1)) 'raise to places + 1
dr = Math.Truncate(r * numD)
If UorD Then 'no adjustment for down
'up
dr += 5
End If
dr = Math.Truncate(dr / 10) * 10
dr = dr / r
Catch ex As Exception
Stop
End Try
Return dr
End Function



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: