Question cutting off digits and rounding up/down

Streen

Member
Joined
Apr 7, 2010
Messages
16
Programming Experience
3-5
I need a function that takes a decimal, with a certain number of digits behiond the comma and convert it to a string with a different number of n digits behind the comma. (to display it)
My problem is the rounding ip/down.

like 123,789 with n = 2 --> 123,79
or 123,4 with n = 0 --> 123
or 123,5 with n = 0 --> 124

I did not find an existing function that does that, so I wrote one myself.
However the code has become soo complex and it still does not work well, and I get frustrated to spend soo much time with something so trivial.

Does anyone know a function that can do this? There must be something out there.
 
Here's some code I had laying around to do this.

VB.NET:
Imports System.Runtime.CompilerServices

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim d1 As Double = 0.3125
        Dim d2 As Double = 3000.51
        d1.Round(3)
        d2.Round(1)
    End Sub
End Class

Friend Module MathHelpers
    <Extension()> _
    Public Sub Round(ByRef theDouble As Double, ByVal numDecimalPlaces As Integer)
        theDouble = Math.Round(theDouble - 0.5 / Math.Pow(10, numDecimalPlaces), numDecimalPlaces, MidpointRounding.AwayFromZero)
    End Sub
End Module
 
JohnH's post is spot on with included functinality from .Net

Here is a small function that I ported over from a lighter language/framework that does the same thing. The only reason I show it is it also allows rounding to a specific space relative to radix OR base.


VB.NET:
        ''' <summary>
        ''' roundTo some place comparative to a 'base', default is 10 for decimal place
        ''' 
        ''' 'place' is represented by the power applied to 'base' to get that place
        ''' </summary>
        ''' <param name="value">the value to round</param>
        ''' <param name="place">the place to round to</param>
        ''' <param name="base">the base to round in... default is 10 for decimal</param>
        ''' <returns>The value rounded</returns>
        ''' <remarks>e.g.
        ''' 
        ''' 2000/7 ~= 285.714285714285714285714 ~= (bin)100011101.1011011011011011
        ''' 
        ''' roundTo(2000/7,3) == 0
        ''' roundTo(2000/7,2) == 300
        ''' roundTo(2000/7,1) == 290
        ''' roundTo(2000/7,0) == 286
        ''' roundTo(2000/7,-1) == 285.7
        ''' roundTo(2000/7,-2) == 285.71
        ''' roundTo(2000/7,-3) == 285.714
        ''' roundTo(2000/7,-4) == 285.7143
        ''' roundTo(2000/7,-5) == 285.71429
        ''' 
        ''' roundTo(2000/7,3,2)  == 288       -- 100100000
        ''' roundTo(2000/7,2,2)  == 284       -- 100011100
        ''' roundTo(2000/7,1,2)  == 286       -- 100011110
        ''' roundTo(2000/7,0,2)  == 286       -- 100011110
        ''' roundTo(2000/7,-1,2) == 285.5     -- 100011101.1
        ''' roundTo(2000/7,-2,2) == 285.75    -- 100011101.11
        ''' roundTo(2000/7,-3,2) == 285.75    -- 100011101.11
        ''' roundTo(2000/7,-4,2) == 285.6875  -- 100011101.1011
        ''' roundTo(2000/7,-5,2) == 285.71875 -- 100011101.10111
        ''' 
        ''' note what occurs when we round to the 3rd space (8ths place), 100100000, this is to be assumed 
        ''' because we are rounding 100011.1011011011011011 which rounds up.</remarks>
        Public Shared Function roundTo(ByVal value As Double, Optional ByVal place As Integer = 0, Optional ByVal base As UInteger = 10) As Double
            Dim p As Double = Math.Pow(base, -place)

            Return Math.Round(value * p) / p
        End Function

        Public Shared Function floorTo(ByVal value As Double, Optional ByVal place As Integer = 0, Optional ByVal base As UInteger = 10) As Double
            Dim p As Double = Math.Pow(base, -place)
            Return Math.Floor(value * p) / p
        End Function

        Public Shared Function ceilTo(ByVal value As Double, Optional ByVal place As Integer = 0, Optional ByVal base As UInteger = 10) As Double
            Dim p As Double = Math.Pow(base, -place)
            Return Math.Ceiling(value * p) / p
        End Function
 
Back
Top