954,551 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

decimal places in vb

Hi, i have a project to do in VB and its been a while i done anything in it.
My problem is taxation and decimal places.

How can I round 2.19 to 2.20 ?

the actuall number was 2.194 and vb6 rounds it at 2.19, is this as it should behave?

tnx

dixie_flatline
Newbie Poster
3 posts since Apr 2010
Reputation Points: 10
Solved Threads: 0
 

Check out the Round function and the Format function and for how vb rounds, see some of the articles this search brought up...

http://search.yahoo.com/search;_ylt=AsacIZvoNaeCzNBceyH577ObvZx4?p=how+does+vb6+round+numbers&toggle=1&cop=mss&ei=UTF-8&fr=yfp-t-701

Good Luck

vb5prgrmr
Posting Virtuoso
1,912 posts since Mar 2009
Reputation Points: 156
Solved Threads: 296
 

this is enough what vb5prgrmr said.

Attachments howto_round_to_specified_digits.zip (6.87KB)
abu taher
Practically a Posting Shark
845 posts since Jul 2008
Reputation Points: 14
Solved Threads: 78
 

You can either use the format function as in -

txtAnswer.Text = Format(p - (a * f - ct), "##,##0")
'Using the 0(zero) at the end will round the number off to a zero.


Or you can use the round function as in -

Dim TotCost As Single
Dim srTotal As String
Dim Quarters As Integer
'   This routine assumes that you have created a form
'    containg a text box for entering the amount to be rounded called "txtCost",
'    a command button called "cmdRound" to do the calculation,
'    and a label to display the result called "lblRounded".
'    Any amount half a penny above the 25 cent mark will be rounded up
'    to the next quarter dollar amount.

Code

Private Sub cmdRound_Click()

    TotCost = Val(txtCost.Text)  'insert entered value into var
    srTotal = CCur(TotCost) 'round value to two decimals and convert to string
    srTotal = Format(srTotal, "currency") 'force string to give 2 decimal places
   
    'round up to next highest quarter dollar
    If Val(Right$(srTotal, 2)) > 75 Then
        srTotal = Int(srTotal) + 1
    ElseIf Val(Right$(srTotal, 2)) > 50 Then
        Quarters = 3
        srTotal = Int(srTotal) + (0.25 * Quarters)
    ElseIf Val(Right$(srTotal, 2)) > 25 Then
        Quarters = 2
        srTotal = Int(srTotal) + (0.25 * Quarters)
    ElseIf Val(Right(srTotal, 2)) > 0 Then
        Quarters = 1
        srTotal = Int(srTotal) + (0.25 * Quarters)
    End If
    
    srTotal = Format(srTotal, "currency") 'convert string to currency again
    lblRounded.Caption = srTotal            'display rounded value in label
    
End Sub


The exact amount is not always given, generating errors. This is something done a while back which works perfectly.

'Rounding numbers in VB is tricky. Round() function does not round $$ amounts correctly. This function rounds a number (2 decimals) without using the Round() function 

Public Function RoundDollars(piInNumber)
    
Dim intNumber
Dim intDecimalLocation
Dim intDecimalNumber, intOriginalDecimalNumber
Dim intNextToNumber
Dim intOriginalNumber

If IsNumeric(piInNumber) = False Then Exit Function

intOriginalNumber = piInNumber
intNumber = Int(piInNumber)
intDecimalLocation = InStr(1, piInNumber, ".")
piInNumber = Mid(piInNumber, 1, intDecimalLocation + 2)
intDecimalNumber = Mid(piInNumber, intDecimalLocation + 1, 2)
intNextToNumber = Mid(intOriginalNumber, intDecimalLocation + 2 + 1, 1)
    
    
If IsNumeric(intNextToNumber) Then
     If intNextToNumber >= 5 And IsNull(intNextToNumber) = False Then
        intOriginalDecimalNumber = intDecimalNumber
        intDecimalNumber = intDecimalNumber + 1
        If LenB(intDecimalNumber) < LenB(intOriginalDecimalNumber) Then
            intDecimalNumber = "0" & intDecimalNumber
        End If
        If LenB(intDecimalNumber) > LenB(intOriginalDecimalNumber) Then
            intNumber = intNumber + 1
            RoundDollars = intNumber
        Else
            intNumber = intNumber & "." & intDecimalNumber
            RoundDollars = intNumber
        End If
    Else
        RoundDollars = intNumber & "." & intDecimalNumber
    End If
Else
    RoundDollars = intOriginalNumber
End If

End Function


I hope this helps towards you solving your problem.

AndreRet
Senior Poster
3,922 posts since Jan 2008
Reputation Points: 334
Solved Threads: 350
 

tnx guys u have been very helpfull!!!

dixie_flatline
Newbie Poster
3 posts since Apr 2010
Reputation Points: 10
Solved Threads: 0
 

Only a pleasure dixie. Happy coding....

AndreRet
Senior Poster
3,922 posts since Jan 2008
Reputation Points: 334
Solved Threads: 350
 

How about using rnd function

sample:
dim total1 as double,hrs as single, rate as single

total1 = format(rnd(hrs*rate),"###,##0.00")

vbboy
Newbie Poster
7 posts since Apr 2010
Reputation Points: 10
Solved Threads: 1
 

Random???? I guess u ment round? :
Anyways im OK now, everything is cool and working fine!!!

dixie_flatline
Newbie Poster
3 posts since Apr 2010
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: