nbala75 0 Posted December 24, 2010 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
eukalyptus 40 Posted December 24, 2010 (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 December 24, 2010 by eukalyptus DirectSound UDFDirect2D UDFAutoIt ScreenSaver CollectionBASS UDF v10 download Share this post Link to post Share on other sites
smashly 12 Posted December 24, 2010 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
nbala75 0 Posted December 24, 2010 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
saywell 3 Posted December 24, 2010 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
Tvern 11 Posted December 24, 2010 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
UEZ 1,273 Posted December 24, 2010 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