Fancy Number Formatting

Started by KeithW, May 27, 2020, 11:43:54 AM

Previous topic - Next topic

KeithW

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

td

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.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

JTaylor

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


KeithW

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

JTaylor

You might find more of what you want here...

    http://winbatch.hpdd.de/


Jim

td

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.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

KeithW

Tony,

Since Jim posted code, could you provide the URL?

Keith

td

Jim already did    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.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

KeithW

Sorry, missed his following post....

stanl

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

KeithW

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

stanl

You can try 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.

KeithW