jquery-maskmoney icon indicating copy to clipboard operation
jquery-maskmoney copied to clipboard

Problem to get value with "unmasked" when precision is 0

Open atrandafir opened this issue 10 years ago • 13 comments

I have configured it with precision to 0, because I dont want any decimals.

So when I type 555555 it turns into 555.555 €, but when I use "unmasked" it will bring back 555.555 instead of 555555 as it should.

atrandafir avatar Apr 06 '14 11:04 atrandafir

same issue here

pilap82 avatar Apr 14 '14 07:04 pilap82

@atrandafir @pilap82 sorry for taking so long to reply. I will take a look at this in a few days. here's what I'm planning for maskmoney 3.1 https://github.com/plentz/jquery-maskmoney/issues?milestone=6

plentz avatar May 25 '14 17:05 plentz

Same issue. I've got a field with value 3. I am calling $field.maskedMoney({ precision: 0 });..

then, when I do $field.maskedMoney('unmasked')[0] it returns 0.3. Why?

ioleo avatar Nov 05 '14 17:11 ioleo

Hey guys, have their been any update on this situation ? We are suffering from the same issue :/ Cheers

EtienneDepaulis avatar Nov 19 '14 15:11 EtienneDepaulis

@EtienneDepaulis instead jqyery-maskmoney I used RobinHerbots/jquery.inputmask library (for input masking) and leongersen/wnumb for number/money formatting.

ioleo avatar Nov 19 '14 15:11 ioleo

I can at least tell you what the issue is:

  1. the settings are within a closure that is bound to the element with .init, so they currently aren't accessible back to the unmask function.
  2. because of this the unmasked function uses regex "/\D/g" to split numbers by groups of non-numeric characters, and assumes that the last one is the decimal
  3. therefor if you're using precision 0, the unmask will treat your last group after a non-numeric symbol as the decimal. If the value is less than 1000, that could end up returning ""

solution:

  1. make settings available to the unmask function. This should be accomplished by writing the data attributes to field when masking
  2. in unmask function, read the data attributes and use them to run a string.replace(regex,"")

I have mocked some of this up, but the changes are pretty damn extensive.

gbass84 avatar Nov 19 '14 18:11 gbass84

Here's the solution I've been working on:

Move unmasked label in to init method and add an event binding to get a handle back to it. This was so that the unmask function has access to the settings object. Then I think the solution is to check for minus symbol and the index of the decimal/cents separator. Run the existing \D regex, split, and re-insert decimal symbol and -. (should it replace it with "." or with the specified separator?)

In the place of the exiting unmask method, I trigger the unmasked event on the element so that the invocation remains the same.

Any thoughts on this?

gbass84 avatar Nov 21 '14 18:11 gbass84

I have the same issue here. Any plan of fixing it the way @gbass84 suggested ? I'm not good enough at javascript to handle it myself unfortunately...

gael-wogenstahl avatar Feb 26 '15 21:02 gael-wogenstahl

Seeing the same issue, any work-around ideas for getting the correct value? I'm thinking of multiplying by 1,000.

RazorCommerce avatar May 12 '15 12:05 RazorCommerce

@RazorCommerce

I believe I resolved this by utilizing jQuery.data() to bind the settings to the element and then reusing them to determine the decimal value. I've added a pull request for this update, #175

NemSavic avatar May 22 '15 18:05 NemSavic

@NemSavic Your fix works perfectly for me. Thanks

nigelterry avatar May 28 '15 13:05 nigelterry

@NemSavic This is working great. Thank you!

Audesi avatar Sep 30 '15 19:09 Audesi

I created the pull request #214 that complements the @NemSavic pull request #175, correcting tests and the build process.

danieltartarottisobrosa avatar Feb 07 '17 12:02 danieltartarottisobrosa