Framework  3.9
currency_type_renderer.inc
Go to the documentation of this file.
1 <?php
6 /**************************************************************
7 
8  Copyright (c) 2007-2010 Sonjara, Inc
9 
10  Permission is hereby granted, free of charge, to any person
11  obtaining a copy of this software and associated documentation
12  files (the "Software"), to deal in the Software without
13  restriction, including without limitation the rights to use,
14  copy, modify, merge, publish, distribute, sublicense, and/or sell
15  copies of the Software, and to permit persons to whom the
16  Software is furnished to do so, subject to the following
17  conditions:
18 
19  The above copyright notice and this permission notice shall be
20  included in all copies or substantial portions of the Software.
21 
22  Except as contained in this notice, the name(s) of the above
23  copyright holders shall not be used in advertising or otherwise
24  to promote the sale, use or other dealings in this Software
25  without prior written authorization.
26 
27  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
28  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
29  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
30  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
31  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
32  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
33  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
34  OTHER DEALINGS IN THE SOFTWARE.
35 
36 *****************************************************************/
37 
38 require_once realpath(dirname(__FILE__)."/abstract_type_renderer.inc");
39 
41 {
42 
44  {
45  }
46 
47  /*
48  * For currency formatting, the template should be
49  * a number, reprenting the decimal parameter to the
50  * function number_format.
51  *
52  * If no template, formats with 2 decimals.
53  *
54  * A future enhancement would be to allow for
55  * additional decimal format options such as
56  * placement of ",". However, it can wait until needed.
57  *
58  * http://php.net/manual/en/function.number-format.php
59  *
60  * For example:
61  * {amount:0} returns 89
62  * {amount:1} returns 89.3
63  * {amount:2} returns 89.32
64  */
65  static function format($value, $template = "")
66  {
67  $template = trim($template);
68 
69  if ($value == "" || !isset($value) || !is_numeric($value))
70  {
71  $value = "0";
72  }
73 
74  if ($template == "numeric")
75  {
76  $text = number_format($value, 2, ".", "");
77  }
78  else if (startsWith($template, "numeric,"))
79  {
80  $places = substr($template, 8);
81  $text = number_format($value, 2, ".", "");
82  }
83  else if ($template != "" && is_numeric($template))
84  {
85  $text = number_format($value, $template);
86  }
87  else
88  {
89  $text = number_format($value, 2);
90  }
91 
92  return $text;
93  }
94 }
95 
static format($value, $template="")
startsWith($text, $start)
Tests whether a string starts with a given sub-string.
Definition: functions.inc:1470