Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
var currencyValidator = {
format: function (number) {
return (Math.trunc(number * 1000000000000) / 1000000000000).toFixed(2)
},
parse: function (newString, oldNumber) {
var CleanParse = function (value) {
return { value: value }
}
var CurrencyWarning = function (warning, value) {
return {
warning: warning,
value: value,
attempt: newString
}
}
var NotAValidDollarAmountWarning = function (value) {
return new CurrencyWarning(newString + ' is not a valid dollar amount', value)
}
var AutomaticConversionWarning = function (value) {
return new CurrencyWarning(newString + ' was automatically converted to ' + value, value)
}
var newNumber = Number(newString)
var indexOfDot = newString.indexOf('.')
var indexOfE = newString.indexOf('e')
if (isNaN(newNumber)) {
if (
indexOfDot === -1 &&
indexOfE > 0 &&
indexOfE === newString.length - 1 &&
Number(newString.slice(0, indexOfE)) !== 0
) {
return new CleanParse(oldNumber)
} else {
return new NotAValidDollarAmountWarning(oldNumber)
}
}
var newCurrencyString = currencyValidator.format(newNumber)
var newCurrencyNumber = Number(newCurrencyString)
if (newCurrencyNumber === newNumber) {
if (indexOfE !== -1 && indexOfE === newString.length - 2) {
return new AutomaticConversionWarning(newNumber)
} else {
return new CleanParse(newNumber)
}
} else {
return new NotAValidDollarAmountWarning(
newNumber > newCurrencyNumber
? newCurrencyNumber
: oldNumber
)
}
}
}
@bcoppingHOF

This comment has been minimized.

Show comment
Hide comment
@bcoppingHOF

bcoppingHOF Jan 6, 2017

Hi, I've been looking at the vue documentation where they utilise your currency validator here -
https://vuejs.org/v2/guide/migration.html#Filters-Outside-Text-Interpolations-removed

I noticed a weird bug when playing about with the currency inputs in the examples.
For example if I try entering 33.33 It will round down to 33.29, making it impossible to enter that amount.

It also happens trying to enter 33.80, rounds down to 33.79, haven't noticed any others :)

Hi, I've been looking at the vue documentation where they utilise your currency validator here -
https://vuejs.org/v2/guide/migration.html#Filters-Outside-Text-Interpolations-removed

I noticed a weird bug when playing about with the currency inputs in the examples.
For example if I try entering 33.33 It will round down to 33.29, making it impossible to enter that amount.

It also happens trying to enter 33.80, rounds down to 33.79, haven't noticed any others :)

@shibamo

This comment has been minimized.

Show comment
Hide comment
@shibamo

shibamo Feb 11, 2017

@bcoppingHOF,
Try change line 3 to:
return (Math.trunc(number * 1000) / 1000).toFixed(2)

shibamo commented Feb 11, 2017

@bcoppingHOF,
Try change line 3 to:
return (Math.trunc(number * 1000) / 1000).toFixed(2)

@simonwu-os

This comment has been minimized.

Show comment
Hide comment
@simonwu-os

simonwu-os Feb 25, 2017

@shibamo works now for 33.33.

@shibamo works now for 33.33.

@rambii

This comment has been minimized.

Show comment
Hide comment
@rambii

rambii Mar 24, 2017

I noticed another bug where I enter 1.11 and afterwards add more 1s to the beginning until it will parse to 1111.1
The solution of @shibamo solves it for small numbers, but it will appear again for large numbers.

Thanks for the snippets though :)

rambii commented Mar 24, 2017

I noticed another bug where I enter 1.11 and afterwards add more 1s to the beginning until it will parse to 1111.1
The solution of @shibamo solves it for small numbers, but it will appear again for large numbers.

Thanks for the snippets though :)

@adamcarheden

This comment has been minimized.

Show comment
Hide comment
@adamcarheden

adamcarheden Apr 12, 2017

In the interest of shameless self-promotion, I deal with some of those bugs at https://github.com/adamcarheden/html-form-tools. vue-compatible version coming soon (maybe).

In the interest of shameless self-promotion, I deal with some of those bugs at https://github.com/adamcarheden/html-form-tools. vue-compatible version coming soon (maybe).

@GoTim

This comment has been minimized.

Show comment
Hide comment
@GoTim

GoTim Oct 12, 2017

is perfect and practical code, start it ~
i think adding annotation is a great idea! thanks ~

GoTim commented Oct 12, 2017

is perfect and practical code, start it ~
i think adding annotation is a great idea! thanks ~

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment