Skip to content
This repository
Browse code

Fix for showing negative numbers. Also accepts more input formats. Fi…

…xes #402
  • Loading branch information...
commit 8614336df1c6c12743fb6ab983eaee12f9d076dd 1 parent 4d66301
Marc Worrell authored September 06, 2012
30  modules/mod_base/filters/filter_format_price.erl
@@ -43,14 +43,16 @@ insert_thousands_separator(Sep, Output, Input) when is_list(Input) ->
43 43
 insert_thousands_separator(Sep, Input) when is_integer(Input) ->
44 44
     insert_thousands_separator(Sep, [], integer_to_list(Input)).
45 45
 
  46
+format_price(Input, DSep, TSep, Context) when is_integer(Input), Input < 0 ->
  47
+    iolist_to_binary([$-, format_price(-Input, DSep, TSep, Context)]);
46 48
 format_price(Input, DSep, TSep, _Context) when is_integer(Input) ->
47 49
     case Input rem 100 of
48 50
         0 -> 
49  
-            [insert_thousands_separator(TSep, Input div 100), DSep, $0, $0 ];
  51
+            iolist_to_binary([insert_thousands_separator(TSep, Input div 100), DSep, $0, $0 ]);
50 52
         Cents when Cents < 10 -> 
51  
-            [insert_thousands_separator(TSep, Input div 100), DSep, $0, Cents + $0 ];
  53
+            iolist_to_binary([insert_thousands_separator(TSep, Input div 100), DSep, $0, Cents + $0 ]);
52 54
         Cents -> 
53  
-            [insert_thousands_separator(TSep, Input div 100), DSep, integer_to_list(Cents) ]
  55
+            iolist_to_binary([insert_thousands_separator(TSep, Input div 100), DSep, integer_to_list(Cents) ])
54 56
     end;
55 57
 format_price(Input, DSep, TSep, Context) when is_float(Input) ->
56 58
     format_price(round(Input * 100), DSep, TSep, Context);
@@ -58,18 +60,18 @@ format_price(Input, DSep, TSep, Context) when is_function(Input, 0) ->
58 60
     format_price(Input(), DSep, TSep, Context);
59 61
 format_price(Input, DSep, TSep, Context) when is_function(Input, 1) ->
60 62
     format_price(Input(Context), DSep, TSep, Context);
61  
-format_price(Input, DSep, TSep, Context) when is_list(Input) ->
62  
-    case string:to_integer(Input) of
63  
-        {error, _} -> Input;
64  
-        {N, _Rest} -> format_price(N, DSep, TSep, Context)
65  
-    end;
66  
-format_price(Input, DSep, TSep, Context) when is_binary(Input) ->
67  
-    case string:to_integer(binary_to_list(Input)) of
68  
-        {error, _} -> Input;
69  
-        {N, _Rest} -> format_price(N, DSep, TSep, Context)
70  
-    end;
  63
+format_price({trans, _} = Tr, DSep, TSep, Context) ->
  64
+    format_price(z_trans:lookup_fallback(Tr, Context), DSep, TSep, Context);
71 65
 format_price(undefined, _Dsep, _Tsep, _Context) ->
72  
-    "-".
  66
+    undefined;
  67
+format_price(<<>>, _Dsep, _Tsep, _Context) ->
  68
+    undefined;
  69
+format_price([], _Dsep, _Tsep, _Context) ->
  70
+    undefined;
  71
+format_price(Input, DSep, TSep, Context) ->
  72
+    format_price(z_convert:to_integer(Input), DSep, TSep, Context).
  73
+
  74
+
73 75
 
74 76
 format_price(Input, Args, Context) ->
75 77
     case length(Args) of

0 notes on commit 8614336

Please sign in to comment.
Something went wrong with that request. Please try again.