MyEarth Posted February 3, 2014 Share Posted February 3, 2014 Hi, I have $var can be: 0 0.5236 123.682225 1.6335896336 etc. I'd like to have only three decimal afer the dot without round the number. i have see on the help and i think i need to use StringFormat but i don't have understand how to use it, please point me in the right direction. Thanks Link to comment Share on other sites More sharing options...
Unc3nZureD Posted February 3, 2014 Share Posted February 3, 2014 (edited) Try this one: ________________________________________________________________________________ MsgBox(0,"", ExactRound("1.6335896336", 3)) Func ExactRound($var, $length) If Not StringInStr($var, ".") Then SetError(1, 0, 0) $a = StringSplit($var, ".") Return $a[1] & "." & StringLeft($a[2], $length) EndFunc ________________________________________________________________________________ Edited February 3, 2014 by Unc3nZureD Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted February 3, 2014 Moderators Share Posted February 3, 2014 MyEarth,This works for me: Global $aNumbers[4] = [0, 0.5236, 123.682225, 1.6335896336] For $i = 0 To 3 ConsoleWrite(StringFormat("%.3f", $aNumbers[$i]) & @CRLF) NextM23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
Malkey Posted February 3, 2014 Share Posted February 3, 2014 (edited) This example doesn't round up. Global $aNumbers[4] = [0, 0.5, 234.56789, 1.6336896336] For $i = 0 To 3 ConsoleWrite(StringRegExpReplace($aNumbers[$i] & "000", "^(\d*)\.?(\d{3}).*$", "\1.\2") & @LF) Next Edit: Changed StringRegExpReplace($aNumbers[$i], ".d*$", "") & StringRegExpReplace($aNumbers[$i] & "000", "^.d*(.d{3}).*$", "1") to StringRegExpReplace($aNumbers[$i] & "000", "^(d*.d{3}).*$", "1") Edit2: Removed If StringInStr($aNumbers[$i], ".") = 0 Then $aNumbers[$i] &= "." Edit3: This example give a joice of trailing zeros or not. Global $aNumbers[5] = [0, .1234, 1.5, 0.54321, 34.5678] For $i = 0 To UBound($aNumbers) - 1 ConsoleWrite($aNumbers[$i] & @TAB & " >> " & @TAB & _NumberDecPlaces($aNumbers[$i], 3, 1) & @TAB & _NumberDecPlaces($aNumbers[$i], 3, 0) & @LF) Next ;$bTraingZeros -(Default = 1) Show trailing zeros to number of places. Func _NumberDecPlaces($iNum, $iNumOfPlaces, $bTraingZeros = 1) If $bTraingZeros Then Return Execute(StringRegExpReplace($aNumbers[$i], "^(\d*)\.?(\d*).*$", '"\1" & ($iNumOfPlaces = 0 ? "" : ".") & Stringleft("${2}00000000000000", $iNumOfPlaces)')) Else Return Execute(StringRegExpReplace($aNumbers[$i], "^(\d+)\.?(\d{" & $iNumOfPlaces & "}).*$", '"\1" & ($iNumOfPlaces = 0 ? "" : ".") & "${2}"')) EndIf EndFunc ;==>_NumberDecPlaces Edited February 4, 2014 by Malkey Link to comment Share on other sites More sharing options...
mikell Posted February 3, 2014 Share Posted February 3, 2014 This works too - but 0.5 remains 0.5 with one decimal StringRegExpReplace($aNumbers[$i], "(\.\d{3}).*", "$1") Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now