www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - number formatting

reply "steven kladitis" <steven_kladitis yahoo.com> writes:
How do you format numbers to have things like.
  Leading $ or , or <CR> with or without leading zeros.
   for example $56.00
               $056.00
               $1,3456.67
               <345.89>CR
Apr 20 2014
parent reply "steven kladitis" <steven_kladitis yahoo.com> writes:
Note sure if you can edit messages once sent.

  $13,456.67
  245,678,541


On Sunday, 20 April 2014 at 12:50:52 UTC, steven kladitis wrote:
 How do you format numbers to have things like.
  Leading $ or , or <CR> with or without leading zeros.
   for example $56.00
               $056.00
               $1,3456.67
               <345.89>CR
Apr 20 2014
next sibling parent "monarch_dodra" <monarchdodra gmail.com> writes:
On Sunday, 20 April 2014 at 12:53:11 UTC, steven kladitis wrote:
 Note sure if you can edit messages once sent.

  $13,456.67
  245,678,541


 On Sunday, 20 April 2014 at 12:50:52 UTC, steven kladitis wrote:
 How do you format numbers to have things like.
 Leading $ or , or <CR> with or without leading zeros.
  for example $56.00
              $056.00
              $1,3456.67
              <345.89>CR
Simply add what you want in the format string. For example: double d = 56.55; writefln("$03.5s", d); writefln("<.4s>CR", d); will print "$056.55" "<56.55>CR" I don't know of any "built-in" way to do number grouping. Also, when dealing with monetary amounts, you shouldn't be using doubles (I'm not saying you are), but some other structure specifically designed to track cents. Ideally, such a structure would have built-in "toString" formating.
Apr 20 2014
prev sibling parent "JR" <zorael gmail.com> writes:
On Sunday, 20 April 2014 at 12:53:11 UTC, steven kladitis wrote:
 Note sure if you can edit messages once sent.

  $13,456.67
  245,678,541


 On Sunday, 20 April 2014 at 12:50:52 UTC, steven kladitis wrote:
 How do you format numbers to have things like.
 Leading $ or , or <CR> with or without leading zeros.
  for example $56.00
              $056.00
              $1,3456.67
              <345.89>CR
As for grouping by thousands http://dpaste.dzfl.pl/bddb71eb75bb *does* work, but I'm not particularly happy with the approach. As monarch_dodra said, representing money via a struct or similar would be wiser than dealing with raw doubles/reals (as that paste does).
Apr 20 2014