Maximum and Minimum values for ints

Python 3 In Python 3, this question doesn’t apply. The plain int type is unbound. However, you might actually be looking for information about the current interpreter’s word size, which will be the same as the machine’s word size in most cases. That information is still available in Python 3 as sys.maxsize, which is the … Read more

Display number with leading zeros

In Python 2 (and Python 3) you can do: number = 1 print(“%02d” % (number,)) Basically % is like printf or sprintf (see docs). For Python 3.+, the same behavior can also be achieved with format: number = 1 print(“{:02d}”.format(number)) For Python 3.6+ the same behavior can be achieved with f-strings: number = 1 print(f”{number:02d}”)

Generating random whole numbers in JavaScript in a specific range

There are some examples on the Mozilla Developer Network page: /** * Returns a random number between min (inclusive) and max (exclusive) */ function getRandomArbitrary(min, max) { return Math.random() * (max – min) + min; } /** * Returns a random integer between min (inclusive) and max (inclusive). * The value is no lower than … Read more

How to print a number with commas as thousands separators in JavaScript

I used the idea from Kerry’s answer, but simplified it since I was just looking for something simple for my specific purpose. Here is what I have: function numberWithCommas(x) { return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, “,”); } function numberWithCommas(x) { return x.toString().replace(/\B(?<!\.\d*)(?=(\d{3})+(?!\d))/g, “,”); } function test(x, expect) { const result = numberWithCommas(x); const pass = result === expect; … Read more

How do I convert a String to an int in Java?

String myString = “1234”; int foo = Integer.parseInt(myString); If you look at the Java documentation you’ll notice the “catch” is that this function can throw a NumberFormatException, which you can handle: int foo; try { foo = Integer.parseInt(myString); } catch (NumberFormatException e) { foo = 0; } (This treatment defaults a malformed number to 0, … Read more

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