Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem with small numbers ? #9

Open
Mouish opened this issue Dec 9, 2012 · 7 comments
Open

Problem with small numbers ? #9

Mouish opened this issue Dec 9, 2012 · 7 comments

Comments

@Mouish
Copy link

Mouish commented Dec 9, 2012

Hi,

When I do this :
$("#field").autoNumericSet(.000001);
I get 0.00 in field, all right.

But when I do this :
$("#field").autoNumericSet(.0000001);
I get 17.00 !!! in field...
Yep, .0000001 = 1e-7 ... which becomes 17 !

@BobKnothe
Copy link
Collaborator

Mouish,

Thanks for the heads up - I will look for a fix.

Bob

On 12/9/2012 5:53 AM, Mouish wrote:

Hi,

When I do this :
$("#field").autoNumericSet(.000001);
I get 0.00 in field, all right.

But when I do this :
$("#field").autoNumericSet(.0000001);
I get 17.00 !!! in field...
Yep, .0000001 = 1e-7 ... which becomes 17 !


Reply to this email directly or view it on GitHub
#9.

@BobKnothe
Copy link
Collaborator

Mouish,

The problem is autoNumeric treats the input as a string so is remove non
numeric characters from the string "1e-7" becomes "17".

If you use a string that represents a number
$("#field").autoNumericSet(".000001"); you will get the proper results 0.00.

That may not be practical in your case, so I found a solution and can
modify the plugin but need to know which version you are using.

Let me know and I will help.

Bob

On 12/9/2012 5:53 AM, Mouish wrote:

Hi,

When I do this :
$("#field").autoNumericSet(.000001);
I get 0.00 in field, all right.

But when I do this :
$("#field").autoNumericSet(.0000001);
I get 17.00 !!! in field...
Yep, .0000001 = 1e-7 ... which becomes 17 !


Reply to this email directly or view it on GitHub
#9.

@Mouish
Copy link
Author

Mouish commented Dec 16, 2012

Bob,

First of all, thanks for your time.

I use version 1.7.5.
I have solved my issue patching autoNumeric-1.7.5.js, by adding the following first line of the autoNumericSet function (line 882) to round the result to 0 in these cases :

$.fn.autoNumericSet = function (iv) {

    // The line I add :
    if(Math.abs(iv)<.000001){iv=0;}

    if (arguments[1]) {
        return this.val($.autoNumeric.Format(this, iv, arguments[1]));
    }
    return this.val($.fn.autoNumeric.Format(this, iv));
};

My problem is solved, this solution is very satisfying in my case (and I think in many).

About this :
If you use a string that represents a number
$("#field").autoNumericSet(".000001"); you will get the proper results 0.00.

Yes, but it's not very useful because .toString() brings the problem back :

v = 0.0000001; // in most (my) case this is the result of a calculation
v = v.toString(); // you get "1e-7" here...
$("#field").autoNumericSet(v); // so you get 17.00 here...

The problem happens only with numbers < 0.000001 :

var v = 0.000001;
v = v.toString(); // you get "0.000001" here, no problem for autoNumericSet

I think the proper patch would be to modify autoNumeric to deal this numbers like "-1.257814e-7", but it seams quite complicated (adapt all regexp and so...), and not very useful as these numbers will probably finally be formatted "0.00" by autoNumeric...

So maybe my patch is not so bad, and anyway much better than getting 17.00 (or whatever) instead of 0.00, which is a real bug.

Hope this help.

@BobKnothe
Copy link
Collaborator

Mouish,

I was think of approaching this from a different angle. Consider the
following:

         if (value < .000001){
             value = (value + 1).toString(); // add 1 which forces 

the e//xponential notation to decimal "1.0000001" then use toString()
method.
value = value.substring(1); // finally trim the one
from the string this
}
This way you can still use the various rounding methods.

Either way I am glad you found a solution.

Best regards,

Bob

On 12/16/2012 4:24 PM, Mouish wrote:

Bob,

First of all, thanks for your time.

I use version 1.7.5.
I have solved my issue patching autoNumeric-1.7.5.js, by adding the
following first line of the autoNumericSet function (line 882) to
round the result to 0 in these cases :

|$.fn.autoNumericSet = function (iv) {

 // The line I add :
 if(Math.abs(iv)<.000001){iv=0;}

 if (arguments[1]) {
     return this.val($.autoNumeric.Format(this, iv, arguments[1]));
 }
 return this.val($.fn.autoNumeric.Format(this, iv));

};
|

My problem is solved, this solution is very satisfying in my case (and
I think in many).

About this :
If you use a string that represents a number
$("#field").autoNumericSet(".000001"); you will get the proper results
0.00.

Yes, but it's not very useful because .toString() brings the problem
back :

|v = 0.0000001; // in most (my) case this is the result of a calculation
v = v.toString(); // you get "1e-7" here...
$("#field").autoNumericSet(v); // so you get 17.00 here...
|

The problem happens only with numbers < 0.000001 :

|var v = 0.000001;
v = v.toString(); // you get "0.000001" here, no problem for autoNumericSet
|

I think the proper patch would be to modify autoNumeric to deal this
numbers like "-1.257814e-7", but it seams quite complicated (adapt all
regexp and so...), and not very useful as these numbers will probably
finally be formatted "0.00" by autoNumeric...

So maybe my patch is not so bad, and anyway much better than getting
17.00 (or whatever) instead of 0.00, which is a real bug.

Hope this help.


Reply to this email directly or view it on GitHub
#9 (comment).

@Mouish
Copy link
Author

Mouish commented Jan 4, 2013

Good idea, but take care of negative numbers :

(0.000000001+1).toString().substring(1) => ".000000001"
(-0.000000001+1).toString().substring(1) => ".999999999"

@BobKnothe
Copy link
Collaborator

M.

Yes I already addressed that by doing this - i should have emailed you
eariler!

         if (value < 0.000001 && value > 0) {
             value = (value + 1).toString();
             value = value.substring(1);
         }
         if (value < 0 && value > -1) {
             value = (value - 1).toString();
             value = '-' + value.substring(2);
         }

Bob

On 1/4/2013 8:38 AM, Mouish wrote:

Good idea, but take care of negative numbers :

(0.000000001+1).toString().substring(1) => ".000000001"
(-0.000000001+1).toString().substring(1) => ".999999999"


Reply to this email directly or view it on GitHub
#9 (comment).

@BobKnothe
Copy link
Collaborator

M.

I also made a couple other changes to version 1.8.0

http://www.decorplanit.com/plugin/autoNumeric-1.8.0.htm

Hope to release this weekend.

Bob

On 1/4/2013 8:38 AM, Mouish wrote:

Good idea, but take care of negative numbers :

(0.000000001+1).toString().substring(1) => ".000000001"
(-0.000000001+1).toString().substring(1) => ".999999999"


Reply to this email directly or view it on GitHub
#9 (comment).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants