Sign in to follow this  
Followers 0
nbala75

Rounding to 5 cents

7 posts in this topic

Is there a way to round to 5 cents

For eg if the output is $1.87 cents, it has to be rounded to 1.85

Like wise

88 to 90

92 to 90

93 to 95

97 to 95

98 to next $

etc

Thanx

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Is there a way to round to 5 cents

For eg if the output is $1.87 cents, it has to be rounded to 1.85

Like wise

88 to 90

92 to 90

93 to 95

97 to 95

98 to next $

etc

Thanx

For $i = 0 To 2 Step 0.01
    $i = Round($i, 2)
    $iVal = _Round($i)
    ConsoleWrite($i & " = " & $iVal & @CRLF)
Next


Func _Round($iVal)
    $iVal = Round($iVal * 100)
    Local $iTemp = $iVal - Mod($iVal, 5)
    If Mod($iVal, 5) >= 3 Then $iTemp += 5
    $iTemp /= 100
    Return StringFormat('%.2f', $iTemp)
EndFunc   ;==>_Round
Edited by eukalyptus

Share this post


Link to post
Share on other sites

Hi,

Dim $aAmount[5] = [200.88, 200.92, 200.93, 200.97, 200.98]

For $i = 0 To 4
    MsgBox(64, "Amount that's being rounded: $" & $aAmount[$i], "Rounded to the nearest 5 cents: $" & _RoundTo5Cent($aAmount[$i]))
Next

Func _RoundTo5Cent($iAmount)
    Switch StringRight($iAmount, 1)
        Case 1, 6
            $iAmount -= .01
        Case 2, 7
            $iAmount -= .02
        Case 3, 8
            $iAmount += .02
        Case 4, 9
            $iAmount += .01
    EndSwitch
    Return StringFormat("%.2f", $iAmount)
EndFunc

There's probably an easier way, but I had a shot at it ..lol

Cheers

Share this post


Link to post
Share on other sites

For $i = 0 To 2 Step 0.01
    $i = Round($i, 2)
    $iVal = _Round($i)
    ConsoleWrite($i & " = " & $iVal & @CRLF)
Next


Func _Round($iVal)
    $iVal = Round($iVal * 100)
    Local $iTemp = $iVal - Mod($iVal, 5)
    If Mod($iVal, 5) >= 3 Then $iTemp += 5
    $iTemp /= 100
    Return StringFormat('%.2f', $iTemp)
EndFunc   ;==>_Round

Thank u Euk

Btw some times Round(382.025, 2) is returned as 382.02 instead of 382.03...why??

Share this post


Link to post
Share on other sites

Thank u Euk

Btw some times Round(382.025, 2) is returned as 382.02 instead of 382.03...why??

Are you expecting fractions of a cent?

If so using stringsplit - StringSplit ( "string", "delimiters" [, flag ] ) - with the decimal point as the delimiter might be better. Then use the numerical value of the second second element [the part after the dec point]. That will cope with any level of decimal places.

William

Share this post


Link to post
Share on other sites

I'd use somthing like this:

Func _Round($iNum)
    Return StringFormat("%.2f", Round($iNum*20)/20)
EndFunc

If needed with some added input checking.

(Depending on your implementation you might want to remove the stringformat)

Share this post


Link to post
Share on other sites

Try this version:

For $i = 0.85 To 2.10 Step 0.01
    $iVal = _Round5($i)
    ConsoleWrite($i & " = " & $iVal & @CRLF)
Next

Func _Round5($iVal)
    Local $i
    $i = Round(Mod($iVal * 100, 5), 0)
    If $i > 2 Then $iVal += 0.05
    Return StringFormat("%.2f", $iVal - $i / 100)
EndFunc   ;==>_Round5

Br,

UEZ


Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0