Project

General

Profile

Defect #251

number.format not working

Added by Patrick Ruhsert about 13 years ago. Updated about 13 years ago.

Status:
Closed
Priority:
Normal
Assignee:
-
Category:
-
Target version:
-
Start date:
03/21/2011
Due date:
% Done:

100%

Estimated time:
Browser (if web client):

Description

Maybe it's me, but I am trying to format a number to the given pattern. I have this:

$number.format($field.number_format, $record[$field.column_name])

$field.number_format is "#,###" and $record[$field.column_name] is an integer field.

Is that suppose to work?

I see this:

3,00 $

I suppose this is the default format assigned.

I am basically playing with the possibilities.

History

#1

Updated by Patrick Talbot about 13 years ago

Integers are not formatted (or rather they are displayed as plain integers). No one required this before.
If you try with a float, double or decimal this should work.

#2

Updated by Patrick Ruhsert about 13 years ago

hm. But they are formatted by the default format, because I see 3,00 $... And I think it is useful to allow that. Image you have a bigger integer and want to have it as #,###. Then you have a year and don't want it to appear as 2.011.

#3

Updated by Patrick Talbot about 13 years ago

Actually bigger integers should appear as-is (2011 will appear as 2011 unless you try to format it).
But you are right, it might be interesting to have them appear with a specific formatting.
Will see how to add that to next version.

#4

Updated by Patrick Talbot about 13 years ago

  • Status changed from New to Closed
  • % Done changed from 0 to 100

Fixed with v1.4.8

#5

Updated by Patrick Ruhsert about 13 years ago

I am still having problems with this. Now I have a double and do:

$number.format("#,##0.00", $position.nettowert)

what I get is

3000,00 $

However, if I do

$number.currency($position.nettoverkaufspreis)

I get

1.000,00 €

What seems fixed, by the way, is the open file issue with the latest release.

#6

Updated by Patrick Talbot about 13 years ago

Where does the $ comes from???

Do you have used a numberFormat defaultParameter somewhere?

Trying to reproduce and I can't... would you have a sample of this behavior?

#7

Updated by Patrick Ruhsert about 13 years ago

In your sample solution, add

context.test = 18687.876867

and in your testTemplate.html add

$number.format("#,##0.00", $test)

What I get is

18687,88 $

without making any further changes to your samples. BTW: This

$number.format("#,##0.00", 99.99)

throws a cantRender exception.

#8

Updated by Patrick Talbot about 13 years ago

OK, so the $ is coming from the default format I set in the onLoad() method, that's fine.

Now, what is happening here is that Velocity is choking on the # signs: he tries to interpret #,##0.00 as directive.

There are ways to escape # and $ signs using \ so \# or \$ could be used in a regular text context, but here the problem is that if you use $number.format("\#,##0.00", $test) then you end up having the extra character in the output, for example \1 8687,88 (because the String is then taken as-is by the NumberFormat tool)

This is why it isn't working, and why you get a CantRender exception as well (you wouldn't have the exception it if you were to use $!number.format("#,##0.00", 99.99) but then 99.99 wouldn't be in the output at all).

A solution to this problem is to add a variable in your context which will hold the format, so for example:
context.frmt = "#,##0.00";
context.test = 18687.876867;

Then in the template you will be able to use:
$number.format($frmt, $test)
$number.format($frmt, 99.99)

#9

Updated by Patrick Ruhsert about 13 years ago

Sounds logical, everything. I now provide several formats to any report via the context object and it works nicely that way. I think we should mention this in the FAQs maybe?

#10

Updated by Patrick Talbot about 13 years ago

Patrick Ruhsert wrote:

I think we should mention this in the FAQs maybe?

Good point. I will add that. Thanks.

Also available in: Atom PDF