Decimal number is not a number!

You have an input field, where user can enter decimal values. Problem is, that some cultures use decimal point “.” and some uses decimal comma “,”. So what is a valid number in one culture, is not a number at all in a different culture. You can see the full list of differences on wikipedia. There is no built-in validator for numbers in Angular, so we have built our own like this: import { AbstractControl, ValidatorFn } from ‘@angular/forms’; export class NumberValidators { static isNumber(): ValidatorFn { return (c: AbstractControl): { [key: string]: boolean } | null => { if (isNaN(c.value)) { return { …

Continue Reading

Javascript math. LOL

How much is 77.9 – 70? Usually it is 7.9. Right? But not so in Javascript. In Javascript it is 7.900000000000006. Funny as hell 🙂 You can “fix” this by calling .toFixed() on the result number, but it does not fix the cause. As far as I know you can’t fix it, it is part of the language. Read more on StackOverflow Just for case that your browser doesn’t support the .toFixed() method here it is. if (!Number.prototype.toFixed) { Number.prototype.toFixed = function(value, precision) { ///<summary>Returns fixed number of decimal values</summary> var power = Math.pow(10, precision || 0); return String(Math.round(value * power) …

Continue Reading