locbet / jquery-numberformatter

Automatically exported from code.google.com/p/jquery-numberformatter
0 stars 0 forks source link

Allow for square formatting #73

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
Eg. I want to display an area as "100 m2".

Currently, the parseNumber function doesn't recognize that the 2 at the end of 
m2 is not part of the number and thus adds it to the number.

I've modified the function with a part of the formatNumber function removing 
the prefix and suffix of the number string (if it exists).

See attached diff against v1.2.3

Original issue reported on code.google.com by macjo...@gmail.com on 14 Nov 2012 at 2:40

GoogleCodeExporter commented 8 years ago
[deleted comment]
GoogleCodeExporter commented 8 years ago
Making a mess of it. Now the correct diff

Original comment by macjo...@gmail.com on 14 Nov 2012 at 2:45

Attachments:

GoogleCodeExporter commented 8 years ago
New version fixing the hasPercent variable (which should be tested for first...)

Note that I haven't tested this with negative formats like -$23.32

Original comment by macjo...@gmail.com on 14 Nov 2012 at 3:23

Attachments:

GoogleCodeExporter commented 8 years ago
Not quite sure I am reproducing it the same as you, but this is already 
supported functionality that appears to work ok -

var result = $.formatNumber(100, {format: '#,##0 m2', locale: 'en'});

produces result = "100 m2"

perhaps I am missing something?

Original comment by apar...@gmail.com on 22 Oct 2013 at 12:22

GoogleCodeExporter commented 8 years ago
Hi,

I've tested your current script with my form.

The setting is as follows:
I have an input field where users can enter the m2 area of their house. The 
number (eg. 100) is then formatted to "100 m2" on the javascript onBlur.

When the user re-enters the input field and leaves, the formatting is done 
again resulting in:
first time: "1002 m2"
2nd tim: "10022 m2"

etc.

So the issue isn't the first formatting sequence, but the next.

Original comment by joost.de...@gmail.com on 22 Oct 2013 at 1:03

GoogleCodeExporter commented 8 years ago
Ah yes, the problem is to do with the parse and not the format.

To better allow prefix ignoring when parsing, I've added a option on the parse 
called 'allowPostfix' which will truncate the input string as soon as it hits 
something unexpected (space in this example)

which works as follows -

var result = $.parseNumber('100 m2', {format: '#,##0 m2', locale: 'en', 
allowPostfix: true});
// result == '100'

result = $.formatNumber(result, {format: '#,##0 m2', locale: 'en'});
// result == '100 m2'

Which I think solves this issue whilst breaking no existing default 
functionality.

SVN updated and will be included in the 1.2.4 release.

Original comment by apar...@gmail.com on 22 Oct 2013 at 1:23