tommytx Posted March 14, 2013 Posted March 14, 2013 I want to make this number 20244747 look like $20,244,747.00 when printing... Can someone steer me to the section describing that sort of formatting when printing... Actually I will be printing it to an html page inside a table then viewing it via the browser and I want it to look pretty. Thanks
Moderators Melba23 Posted March 14, 2013 Moderators Posted March 14, 2013 tommytx, Here you are: expandcollapse popup$sRaw = "20244747" $sFormat = "$" & _StringAddThousandsSep($sRaw) & ".00" MsgBox(0, "Format", $sFormat) ;=============================================================================== ; Function Name: _StringAddThousandsSep() ; Description: Returns the original numbered string with the Thousands and or Decimal delimiter(s) inserted. ; Syntax: _StringAddThousandsSep($s_string[, $i_convert_lcid = -1[, $i_current_lcid = -1]]) ; Parameter(s): $s_string - The string to be converted. ; $i_convert_lcid - Optional: Default or -1 wll be the User default locale else: ; |LCID of what you want to convert number to. ; $i_current_lcid - Optional: Default or -1 wll be the User default locale else: ; |LCID number was converted with originally. ; Requirement(s): ; Return Value(s): - Success - The string with Thousands delimiter added. ; - Failure - Returns empty string and sets @error to 1. ; Author(s): SmOke_N (orignal _StringAddComma); Valik (original _StringAddThousandsSep) ; GEOSoft (added localaization and ability to handle pre-formatted strings) ; SmOke_N (complete re-write) ; Modification(s): ; Note(s): Changes are script breaking after AutoIt version 3.3.0.0 ; LCID numbers can be obtained by pre-pending the OSLang codes in the help file appendix with "0x". ; This will be the LAST re-write of this function. DO NOT ask for further changes. ; If it's not returning what you want then write your own. ; Example(s): #cs #ce ;=============================================================================== Func _StringAddThousandsSep($s_string, $i_convert_lcid = -1, $i_current_lcid = -1) ; $LOCALE_USER_DEFAULT = 0x0400 If $i_current_lcid = -1 Or $i_current_lcid = Default Then $i_current_lcid = 0x0400 If $i_convert_lcid = -1 Or $i_convert_lcid = Default Then $i_convert_lcid = 0x0400 ; Get lcid decimal and thousands separators Local $t_buff_tmp = DllStructCreate("char[4]") DllCall("kernel32.dll", "int", "GetLocaleInfo", "int", $i_current_lcid, _ "int", 0x0E, "ptr", DllStructGetPtr($t_buff_tmp), "int", 4) If @error Then Return SetError(1, 0, "") Local $s_cur_dec = DllStructGetData($t_buff_tmp, 1) DllCall("kernel32.dll", "int", "GetLocaleInfo", "int", $i_convert_lcid, _ "int", 0x0E, "ptr", DllStructGetPtr($t_buff_tmp), "int", 4) If @error Then Return SetError(1, 0, "") Local $s_con_dec = DllStructGetData($t_buff_tmp, 1) DllCall("kernel32.dll", "int", "GetLocaleInfo", "int", $i_convert_lcid, _ "int", 0x0F, "ptr", DllStructGetPtr($t_buff_tmp), "int", 4) If @error Then Return SetError(1, 0, "") Local $s_con_tho = DllStructGetData($t_buff_tmp, 1) ; For later formatting Local $i_number = StringRegExpReplace($s_string, "(\" & $s_cur_dec & "\d+\z)|(^-|\d+)|(\D)", "$2") Local $i_dec = StringRegExpReplace($s_string, "(.+?\" & $s_cur_dec & ")(\d+\z)", "$2") If @extended = 0 Then $i_dec = "" Local $i_str_len = StringLen($s_string) * 4 Local $t_numberfmt = DllStructCreate("uint;uint;uint;ptr;ptr;uint") Local $t_thousands = DllStructCreate("wchar[2]") Local $t_decimal = DllStructCreate("wchar[2]") Local $t_buffer = DllStructCreate("wchar[" & $i_str_len & "]") DllStructSetData($t_thousands, 1, $s_con_tho) DllStructSetData($t_decimal, 1, $s_con_dec) DllStructSetData($t_numberfmt, 3, 3) DllStructSetData($t_numberfmt, 4, DllStructGetPtr($t_decimal)) DllStructSetData($t_numberfmt, 5, DllStructGetPtr($t_thousands)) DllStructSetData($t_numberfmt, 6, 1) DllCall("kernel32.dll", "int", "GetNumberFormatW", _ "int", $i_convert_lcid, "int", 0, _ "wstr", $i_number, "ptr", DllStructGetPtr($t_numberfmt), _ "ptr", DllStructGetPtr($t_buffer), "int", $i_str_len) If $i_dec = "" Then $s_con_dec = "" Return DllStructGetData($t_buffer, 1) & $s_con_dec & $i_dec EndFunc ;<==> _StringAddThousandsSep() The function used in that script was removed from AutoIt some years ago as there was disagreement over exactly how it should work - fortunately I kept a copy. M23 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
PhoenixXL Posted March 14, 2013 Posted March 14, 2013 (edited) Somewhat like this$Number = "20244747" MsgBox(64, "Formatted Number", FormatNum($Number)) ; #FUNCTION# ==================================================================================================================== ; Name ..........: FormatNum ; Description ...: Format the number with a comma(,) in every $iSkip digit. ; Syntax ........: FormatNum($iNum[, $iDecimal = 2[, $sPrefix = "$"[, $iSkip = 3]]]) ; Parameters ....: $iNum - An integer value. ; $iDecimal - [optional] An integer value. Default is 2. ; $sPrefix - [optional] A string value. Default is "$". ; $iSkip - [optional] An integer value. Default is 3. ; Return values .: Formatted Number as a string. ; Author ........: Phoenix XL ; Modified ......: ; Remarks .......: If $iNum consist of non-numeric values in the beginning, 0 is returned orelse the non-numbers are ignored. ; Related .......: ; Link ..........: ; Example .......: Yes ; =============================================================================================================================== Func FormatNum($iNum, $iDecimal = 2, $sPrefix = "$", $iSkip = 3) If $iDecimal < 1 Then $iDecimal = 2 $iNum = StringFormat("%#." & $iDecimal & "f", $iNum) SetExtended(1) While @extended $iNum = StringRegExpReplace($iNum, '(.*\d)(\d{' & $iSkip & '}[.,]\d*)', "\1,\2", 1) WEnd Return $sPrefix & $iNum EndFunc ;==>FormatNumRegards Edited March 14, 2013 by PhoenixXL My code: PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners. MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.
UEZ Posted March 15, 2013 Posted March 15, 2013 Here another version: #include <String.au3> $i = 123456789 ConsoleWrite("$" & Display1000Seperator($i, ",", 1) & @LF) $i = 9876543210.99 ConsoleWrite(Display1000Seperator($i) & @LF) $i = 100 ConsoleWrite(Display1000Seperator($i) & @LF) $i = "55555" ConsoleWrite(Display1000Seperator($i) & @LF) $i = "99999.123" ConsoleWrite(Display1000Seperator($i) & @LF) Func Display1000Seperator($iNumber, $sSeperator = ",", $bFloat = False) ;coded by UEZ 2012 If $iNumber = "" Then SetError(1, 0, 0) Local $1000 If IsInt(Number($iNumber)) Then $1000 = _StringReverse(StringRegExpReplace(_StringReverse($iNumber), ".{3}", "$0" & $sSeperator)) If StringLeft($1000, 1) = $sSeperator Then $1000 = StringTrimLeft($1000, 1) If $bFloat Then Return $1000 & ".00" Return $1000 ElseIf IsFloat(Number($iNumber)) Then Local $1000 = _StringReverse(StringRegExpReplace(_StringReverse(StringRegExpReplace($iNumber, "(\d+)\..*", "$1")), ".{3}", "$0" & $sSeperator)) If StringLeft($1000, 1) = $sSeperator Then $1000 = StringTrimLeft($1000, 1) Return $1000 & StringRegExpReplace($iNumber, "\d+(\..*)", "$1") EndIf Return SetError(2, 0, 0) EndFunc 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!¯\_(ツ)_/¯ ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ
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