WinBatch® Technical Support Forum

All Things WinBatch => WinBatch => Topic started by: KeithW on May 27, 2020, 11:43:54 AM

Title: Fancy Number Formatting
Post by: KeithW on May 27, 2020, 11:43:54 AM
Greetings,

Is there a function or a code snippet anywhere that handles making numbers user friendly, yes I can write what I need but wondered if something already exists that I have not found in my searches... does things like....

-adding commas to a number   1000 becomes 1,000
-add $ for numbers
- convert negative number "-1" to (1)
etc.

Thanx,
Keith
Title: Re: Fancy Number Formatting
Post by: td on May 27, 2020, 11:50:26 AM
If it's not in the Tech Database, this forum, or the Consolidated WIL Help file, you will likely have to start writing unless some generous user posts a script.
Title: Re: Fancy Number Formatting
Post by: JTaylor on May 27, 2020, 12:13:36 PM
Here is at least part of what you want.   Can't remember where I got this...maybe Detlev or Kirby?

Code (winbatch) Select




#DefineFunction AddCommas(passednumstring)
    ; Grab everything to the left of the decimal (if any)
    intpart=ItemExtract(1,passednumstring, ".")

    ;Grab everything to the right of the decimal
    fractpart=ItemExtract(2,passednumstring,".")

    ;Compute how many comma seperated groups we need
    groups= ((StrCharCount(intpart)-1) / 3 ) +1

    ;Initialize an answer variable to a null string
    answer=""

    ;Figure out what each group looks like
    ;and add each group to the answer variable with
    ;a comma
    For y=1 To groups
       If y==1
          ptr=( (StrCharCount(intpart)-1) mod 3) +1
          answer=StrSub(intpart,1, ptr )
          ptr=ptr +1
       Else
          answer=StrCat(answer, ",", StrSub(intpart, ptr , 3))
          ptr=ptr+3
       EndIf
    Next

    ;Now that we have the numbers to the left of the decimal
    ;point "comma-ized" properly, see if there is any
    ;fractional part to worry about.

    ;If there is a fractional part, glue it onto the end
    ;of he "comma-ized" number along with a decimal
    If fractpart!=""
       answer=StrCat(answer,".",fractpart)
    EndIf

    ;Return the comma-ized answer
    Return (answer)

#EndFunction

Title: Re: Fancy Number Formatting
Post by: KeithW on May 27, 2020, 12:38:22 PM
Thanx Jim....

Tony, I wasn't sure if I was searching under all the correct terms as I had not found what I was looking for.
No more, no less ... thanx

Regards,
Keith
Title: Re: Fancy Number Formatting
Post by: JTaylor on May 27, 2020, 01:11:05 PM
You might find more of what you want here...

    http://winbatch.hpdd.de/


Jim
Title: Re: Fancy Number Formatting
Post by: td on May 27, 2020, 01:35:46 PM
Quote from: KeithW on May 27, 2020, 12:38:22 PM

Tony, I wasn't sure if I was searching under all the correct terms as I had not found what I was looking for.
No more, no less ... thanx


Since there are over 4,000 articles in the Tech Database and over 1,000 examples in Consolidated WIL, I can't claim to know everything that is and isn't covered.  But the only time I recall number formatting being covered (outside of digits past the decimal point) was on the old WinBatch Webboard that has long since gone to the bit bucket.

The URL to Detlev's script library that Jim mentioned is something worth hanging on to.
Title: Re: Fancy Number Formatting
Post by: KeithW on May 27, 2020, 01:42:33 PM
Tony,

Since Jim posted code, could you provide the URL?

Keith
Title: Re: Fancy Number Formatting
Post by: td on May 27, 2020, 02:03:14 PM
Jim already did    http://winbatch.hpdd.de/ (http://winbatch.hpdd.de/.)  I don't know if Detlev has any number formatting UDFs but it is a good reference when looking for WinBatch examples.
Title: Re: Fancy Number Formatting
Post by: KeithW on May 27, 2020, 02:04:16 PM
Sorry, missed his following post....
Title: Re: Fancy Number Formatting
Post by: stanl on May 27, 2020, 04:54:03 PM
it is a bit crude, but vbscript is versatile
Code (WINBATCH) Select


#DefineFunction fmatnum(num)
retval=""
oS = CreateObject("MSScriptControl.ScriptControl")
oS.Language = "VBScript"
oS.AllowUI = @FALSE
retval=oS.Eval(: 'FormatNumber(%num%,0,,-1)')
oS = 0
Return(retval)
#EndFunction


num=455311


Message("Number ":num,"Formatted ":fmatnum(num))


num=-455311


Message("Number ":num,"Formatted ":fmatnum(num))
Title: Re: Fancy Number Formatting
Post by: KeithW on May 27, 2020, 05:25:29 PM
Stan,

That works great, where would I find doc on FormatNumber Function
Never done anything VB, well not since it first came out ions ago.

Keith
Title: Re: Fancy Number Formatting
Post by: stanl on May 28, 2020, 02:29:22 AM
You can try https://www.w3schools.com/asp/asp_ref_vbscript_functions.asp (https://www.w3schools.com/asp/asp_ref_vbscript_functions.asp)


If you check out FormatNumber() you will find it has several parameters which could be included in the udf. There are also functions for currency, percentage and dates.


P.S.  the scriptcontrol also can be set to JScript which gives even more functionality.
Title: Re: Fancy Number Formatting
Post by: KeithW on May 28, 2020, 07:14:03 AM
Stan,

Thanx !!

Keith