Jump to content
Sign in to follow this  

Number of decimal

Recommended Posts



I have $var can be:






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

Share this post

Link to post
Share on other sites

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)



Edited by Unc3nZureD

Share this post

Link to post
Share on other sites


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)

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:


ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area


Share this post

Link to post
Share on other sites

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)

Edit: Changed StringRegExpReplace($aNumbers[$i], ".d*$", "") & StringRegExpReplace($aNumbers[$i] & "000", "^.d*(.d{3}).*$", "1")
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)

;$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)'))
        Return Execute(StringRegExpReplace($aNumbers[$i], "^(\d+)\.?(\d{" & $iNumOfPlaces & "}).*$", '"\1" & ($iNumOfPlaces = 0 ? "" : ".") & "${2}"'))
EndFunc   ;==>_NumberDecPlaces
Edited by Malkey

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  


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.