Adding space between numbers

For integers use function numberWithSpaces(x) { return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ” “); } For floating point numbers you can use function numberWithSpaces(x) { var parts = x.toString().split(“.”); parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ” “); return parts.join(“.”); } This is a simple regex work. https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Regular_Expressions << Find more about Regex here. If you are not sure about whether the number … Read more

Making all numbers negative

A trivial $num = $num <= 0 ? $num : -$num ; or, the better solution, IMHO: $num = -1 * abs($num) As @VegardLarsen has posted, the explicit multiplication can be avoided for shortness but I prefer readability over shortness I suggest to avoid if/else (or equivalent ternary operator) especially if you have to manipulate … Read more

Simplest way of getting the number of decimals in a number in JavaScript [duplicate]

Number.prototype.countDecimals = function () { if(Math.floor(this.valueOf()) === this.valueOf()) return 0; return this.toString().split(“.”)[1].length || 0; } When bound to the prototype, this allows you to get the decimal count (countDecimals();) directly from a number variable. E.G. var x = 23.453453453; x.countDecimals(); // 9 It works by converting the number to a string, splitting at the . … Read more

Checking if string is numeric in dart

This can be simpliefied a bit void main(args) { print(isNumeric(null)); print(isNumeric(”)); print(isNumeric(‘x’)); print(isNumeric(‘123x’)); print(isNumeric(‘123’)); print(isNumeric(‘+123’)); print(isNumeric(‘123.456’)); print(isNumeric(‘1,234.567’)); print(isNumeric(‘1.234,567’)); print(isNumeric(‘-123’)); print(isNumeric(‘INFINITY’)); print(isNumeric(double.INFINITY.toString())); // ‘Infinity’ print(isNumeric(double.NAN.toString())); print(isNumeric(‘0x123’)); } bool isNumeric(String s) { if(s == null) { return false; } return double.parse(s, (e) => null) != null; } false // null false // ” false // ‘x’ false … Read more

php random x digit number

You can use rand() together with pow() to make this happen: $digits = 3; echo rand(pow(10, $digits-1), pow(10, $digits)-1); This will output a number between 100 and 999. This because 10^2 = 100 and 10^3 = 1000 and then you need to subtract it with one to get it in the desired range. If 005 … Read more