vue-numeric icon indicating copy to clipboard operation
vue-numeric copied to clipboard

Wrong behavior when formating string values

Open AlexanderYW opened this issue 6 years ago • 3 comments

Expected Behavior

If you input a String with "20000.00" as value the output value will be 20.000,00

Actual Behavior

Right now when you input a String with "20000.00" as value the output value will be 2.000.000,00

Steps to Reproduce the Problem

https://codepen.io/anon/pen/vRqyay

Specifications

  • Plugin Version: v2.3.0
  • Vue.js Version: v2.5.16
  • Browser: Chrome, Edge
  • OS: Windows 10

AlexanderYW avatar Apr 14 '18 15:04 AlexanderYW

+1 to this issue. It happens in the valueNumber computed property in mounted via process method https://github.com/kevinongko/vue-numeric/blob/f639863c431eaae34c39cf5792e7381603556f0c/src/vue-numeric.vue#L184-L185

When unformat receives "12345.00", it tells accounting to convert string to number assuming the decimal separator is ,, but it isn't. https://github.com/kevinongko/vue-numeric/blob/f639863c431eaae34c39cf5792e7381603556f0c/src/vue-numeric.vue#L364

Workaround, as seen on codepen, is to make the initial value a Number, then pass into the component via v-model.

A possible long-term solution in the component is to make valueNumber detect if this.value is a numeric string (!this.value.toString.test(/^[\d\.]/)) and then convert it to Numeric before passing it to unformat. However, that would not work correctly if value was "1.001" (one thousand and one) with . as thousand separator.

lulessa avatar May 05 '18 03:05 lulessa

Here ist my (short-term) solution:

add

value = parseFloat(value.replace ? value.replace(",",".") : value)

as first line to

methods.unformat() #363

Numeric values from input-fields can now contain comma as decimal-separator and are converted properly if stored as strings

WulfP avatar Jul 04 '18 16:07 WulfP

Same issue here

Please fix it into the component package Might be interesting to let set array of allowed chars for thousand and decimals separator.

scramatte avatar May 08 '20 14:05 scramatte