Sign in to follow this  
Followers 0
MyEarth

Number of decimal

5 posts in this topic

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

Share this post


Link to post
Share on other sites



#2 ·  Posted (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 by Unc3nZureD

Share this post


Link to post
Share on other sites

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)
Next
M23

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

Spoiler

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

#4 ·  Posted (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 by Malkey

Share this post


Link to post
Share on other sites

This works too - but 0.5 remains 0.5 with one decimal

StringRegExpReplace($aNumbers[$i], "(\.\d{3}).*", "$1")

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