For Programmers: Free Programming Magazines  


Home > Archive > Tcl > October 2005 > formatting currency









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author formatting currency
keithv

2005-10-21, 7:03 pm

What's the best way to a 2 decimal place number into standard
currency format of "12.50".

The more general question is: is there a good way to preserve
trailing zeroes when converting a number to a string. (For an
interesting list of where trailing zeroes are important see:
http://www2.hursley.ibm.com/decimal...aq1.html#tzeros)

The best I could come up with for currency is:
set val [format "%03d" [expr {round($val*100)}]]
set val "[string range $val 0 end-2].[string range $val end-1 end]"
alt: regsub {(..)$} $val {.\1} val

Keith

Kaitzschu

2005-10-21, 7:03 pm

On Fri, 21 Oct 2005, keithv wrote:

> What's the best way to a 2 decimal place number into standard currency
> format of "12.50".
>
> The more general question is: is there a good way to preserve trailing
> zeroes when converting a number to a string.


format %.2f $val

--
-Kaitzschu
s="TCL ";while true;do echo -en "\r$s";s=${s:1:${#s}}${s:0:1};sleep .1;done
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com