Remove insignificant trailing zeros from a number?

I had a similar instance where I wanted to use .toFixed() where necessary, but I didn’t want the padding when it wasn’t. So I ended up using parseFloat in conjunction with toFixed. toFixed without padding parseFloat(n.toFixed(4)); Another option that does almost the same thing This answer may help your decision Number(n.toFixed(4)); toFixed will round/pad the … Read more

PHP prepend leading zero before single digit number, on-the-fly [duplicate]

You can use sprintf: http://php.net/manual/en/function.sprintf.php <?php $num = 4; $num_padded = sprintf(“%02d”, $num); echo $num_padded; // returns 04 ?> It will only add the zero if it’s less than the required number of characters. Edit: As pointed out by @FelipeAls: When working with numbers, you should use %d (rather than %s), especially when there is … Read more

python re.sub group: number after \number

The answer is: re.sub(r'(foo)’, r’\g<1>123′, ‘foobar’) Relevant excerpt from the docs: In addition to character escapes and backreferences as described above, \g will use the substring matched by the group named name, as defined by the (?P…) syntax. \g uses the corresponding group number; \g<2> is therefore equivalent to \2, but isn’t ambiguous in a … Read more

How can I check if my python object is a number? [duplicate]

Test if your variable is an instance of numbers.Number: >>> import numbers >>> import decimal >>> [isinstance(x, numbers.Number) for x in (0, 0.0, 0j, decimal.Decimal(0))] [True, True, True, True] This uses ABCs and will work for all built-in number-like classes, and also for all third-party classes if they are worth their salt (registered as subclasses … Read more

Why does the html input with type “number” allow the letter ‘e’ to be entered in the field?

Because that’s exactly how the spec says it should work. The number input can accept floating-point numbers, including negative symbols and the e or E character (where the exponent is the number after the e or E): A floating-point number consists of the following parts, in exactly the following order: Optionally, the first character may … Read more