integer

A Java method to determine if an integer is between a range

Last night I was writing some code for my Android football game, and decided it would be best if I had a between method, so I could write some code like this to show that I wanted to test to see if a number was between an integer range:

if (between(distance, 8, 10)) { ...

That could would be interpreted as, “If the distance is between the values 8 and 10, do whatever is in the code block.” (I can make that code more readable in Scala, but in Java I think that’s the best I can do.)

How do I round a float or double to an integer in Java?

Java math FAQ: How do I round a float or double to an integer in Java?

Solution: Use Math.round() to round a float (or double) to the nearest integer (int) in Java.

You can see how this works in the examples that follow, where the result is shown in the comment after each line:

Safe String to Int conversion in Haskell (a string from the command line)

I was working on a Haskell factorial function (which turned out to be easy) and decided I wanted to write a program to prompt a user at the command line for an Int, and then do the factorial of that Int.

I quickly learned that this required a String to Int conversion, which I wanted to do safely (I didn’t want my program to blow up). Technically what I do is actually a String to Maybe Int conversion, as you can see in this code:

How to generate random numbers, characters, and sequences in Scala alvin May 31, 2015 - 4:00pm

Scala FAQ: How do I generate random numbers (or characters) in Scala, such as when testing an application, performing a simulation, and many other situations?

Solution

Create random numbers with the Scala scala.util.Random class. You can create random integers:

How to use BigInt and BigDecimal when you need very large numbers in Scala

Scala FAQ: I’m writing an application and I need to use very large integer or decimal (floating-point) numbers; what’s the best approach?

Solution

Use the Scala BigInt and BigDecimal classes. You can create a BigInt:

scala> var b = BigInt(1234567890)
b: scala.math.BigInt = 1234567890

or a BigDecimal:

Scala has no ++ or -- operator; how to increment or decrement an integer?

Scala FAQ: Scala doesn't have the ++ and -- operators; are the some similar operators or methods that I can use instead?

Solution

Because val fields are immutable, they can’t be incremented or decremented, but var integer fields can be mutated with Scala’s += and −= methods:

There are no ++ or -- operators in Scala (use += or -=)

In Scala there are no ++ or -- operators. You should instead use the += and -= operators, as shown below. First the += operator:

scala> var i = 1
i: Int = 1

scala> i++
<console>:9: error: value ++ is not a member of Int
              i++
               ^

scala> i += 1

scala> println(i)
2

Next the -= operator:

A PHP preg_match integer regex pattern matching example

PHP integer FAQ: Can you demonstrate a regular expression pattern to match an integer?

Interestingly, while reading a book titled Advanced PHP Programming, the author mentions that the PHP is_int function doesn't work exactly the way you expect it to. It just tests to see whether a given variable is typed as an integer or something else, so if you give it a string like "123", it will return false. This is a bad thing when you're trying to test web form data, for example.

A Perl random integer example

Perl random integer FAQ: Can you provide a Perl random integer example?

In my earlier Perl random tutorial, I described several different ways of generating random numbers in Perl. Today I'd like to share a Perl script I wrote that uses a random integer number to rotate files on this website.

A Perl random integer script example

The following Perl script performs the following functions: