Re: [SQL] Type Conversion: int4 -> Money - Mailing list pgsql-sql

From Kyle Bateman
Subject Re: [SQL] Type Conversion: int4 -> Money
Date
Msg-id 37D92B4D.125D0F59@actarg.com
Whole thread Raw
List pgsql-sql
Herouth Maoz wrote:

> testing=> select num, num * '1'::money from test1;
>    num|?column?
> ------+-----------
>      2|$2.00
>      4|$4.00
>    -14|($14.00)
>     38|$38.00
>    199|$199.00
> 100399|$100,399.00
> (6 rows)
>
> Is that what you wanted?
>
> Herouth
>

>    Perl... I ended up writing my own formatting function... It has one
that
> does ####.## however not one that'll do the nice 123,456.33 ... :)  Do
you know
> if there are any public modules that do such things?

I use this in tcl to format a dollar amount.  Mine runs in the frontend,
but it could easily be made into a pltcl function:

#format for dollars, including commas
#------------------------------------------
#val:   the value as input by the user
proc comma_dollar {val} {
    if {$val == {}} {set val 0.00}
    if {$val < 0} {set sign {-}; set val [expr -$val]} else {set sign
{}}
    set val [format {%#.2f} $val]
    lassign [split $val .] dol cents
    set cdol {}
    for {set len [string length $dol]} {$len > 0} {incr len -3} {
        if {$cdol == {}} {
            set cdol "[string range $dol [expr $len - 3] [expr $len -
1]]"
        } else {
            set cdol "[string range $dol [expr $len - 3] [expr $len -
1]] $cdol
        }
    }
    return "$sign[join $cdol ,].$cents"
}


Attachment

pgsql-sql by date:

Previous
From: "Jackson, DeJuan"
Date:
Subject: RE: [GENERAL][SQL] Getting multiple field unique index to disting uish NULLs.
Next
From: Beth Strohmayer
Date:
Subject: Re: [GENERAL][SQL] Getting multiple field unique index to distinguish NULLs.