CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 1 of 1
  1. #1
    Join Date
    Jul 2019
    Posts
    1

    Mathround function

    I need to create a way to round a specific variable(metertemp and provertemp) to be rounded to either xx.x0 or xx.x5 all other variables can use the current code below.

    My current code is:

    Code:
    Function MathRound(Numeral As Variant, Optional Decimals As Integer, Optional Keep_Trailing_Zeroes As Boolean) As Variant
    
    'Used instead of round funciton to avoid bankers rounding (format does arithmetic rounding)
    Dim Xloop As Integer
    Dim PlaceString As String
    Dim ZeroString As String
    
    If Numeral = 0 Then
        MathRound = 0
        Exit Function
    End If
    
    If Decimals > 0 Then
        'not whole
        For Xloop = 1 To Decimals
            'build custom formatting string
            PlaceString = PlaceString + "#"
            ZeroString = ZeroString + "0"
        Next Xloop
    
        If Keep_Trailing_Zeroes = True Then 'keep trailing zeroes for later use
            MathRound = Format(Numeral, "##############." & ZeroString)
        Else
            MathRound = Format(Numeral, "##############." & PlaceString)
        End If
    Else
        'round whole
        MathRound = Format(Numeral, "##############")
    End If
    
    End Function
    Please help
    Thank you
    Last edited by 2kaud; July 24th, 2019 at 03:34 AM. Reason: Added code tags

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured