Jump to content

Number of decimal


MyEarth
 Share

Recommended Posts

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

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
Link to comment
Share on other sites

  • Moderators

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

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 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

 

Link to comment
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)
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
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...