in python how do I convert a single digit number into a double digits string?

In python 3.6, the fstring or “formatted string literal” mechanism was introduced. f”{a:02}” is the equivalent of the .format format below, but a little bit more terse. python 3 before 3.6 prefers a somewhat more verbose formatting system: “{0:0=2d}”.format(a) You can take shortcuts here, the above is probably the most verbose variant. The full documentation … Read more

Which is best data type for phone number in MySQL and what should Java type mapping for it be?

Strings & VARCHAR. Do not try storing phone numbers as actual numbers. it will ruin the formatting, remove preceding 0s and other undesirable things. You may, if you choose to, restrict user inputs to just numeric values but even in that case, keep your backing persisted data as characters/strings and not numbers. Be aware of … Read more

Sum the digits of a number

Both lines you posted are fine, but you can do it purely in integers, and it will be the most efficient: def sum_digits(n): s = 0 while n: s += n % 10 n //= 10 return s or with divmod: def sum_digits2(n): s = 0 while n: n, remainder = divmod(n, 10) s += … Read more

Controlling number of decimal digits in print output in R

The reason it is only a suggestion is that you could quite easily write a print function that ignored the options value. The built-in printing and formatting functions do use the options value as a default. As to the second question, since R uses finite precision arithmetic, your answers aren’t accurate beyond 15 or 16 … Read more

Get number of digits with JavaScript

length is a property, not a method. You can’t call it, hence you don’t need parenthesis (): function getlength(number) { return number.toString().length; } UPDATE: As discussed in the comments, the above example won’t work for float numbers. To make it working we can either get rid of a period with String(number).replace(‘.’, ”).length, or count the … Read more

Given a number, find the next higher number which has the exact same set of digits as the original number

You can do it in O(n) (where n is the number of digits) like this: Starting from the right, you find the first pair-of-digits such that the left-digit is smaller than the right-digit. Let’s refer to the left-digit by “digit-x”. Find the smallest number larger than digit-x to the right of digit-x, and place it … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)