Skip to main content Link Menu Expand (external link) Document Search Copy Copied

Working with numbers

Header Image

Ahoy there, matey! Are you ready to set sail on a new adventure in programming? Today, we’re going to be talking about number manipulation methods in Apache Commons Lang.

Whether you’re a seasoned pirate programmer or just starting out on your journey, you’re bound to encounter situations where you need to manipulate numbers in your code. And that’s where Apache Commons Lang comes in handy. With its arsenal of number manipulation methods, you’ll be able to handle even the trickiest of numerical problems with ease.

So, hoist the anchor and let’s dive into the world of number manipulation methods in Apache Commons Lang!

Number manipulation methods

When it comes to manipulating numbers in Apache Commons Lang, you’ve got plenty of tools at your disposal. From rounding and formatting to conversion and comparison, there’s a method for just about every numerical task you can think of.

Let’s take a look at some of the most commonly used number manipulation methods in Apache Commons Lang:

Rounding

Sometimes, you need to round a number to a certain number of decimal places. Apache Commons Lang has got you covered with the NumberUtils.round() method. This method takes a number and the number of decimal places to round to, and returns the rounded value.

double num = 3.14159265359;
double roundedNum = NumberUtils.round(num, 2); // Returns 3.14

Formatting

Formatting numbers can be a bit tricky, especially when you need to take into account locale-specific settings. Apache Commons Lang simplifies this task with the NumberUtils.format() method. This method takes a number, a Locale object, and a NumberFormat object, and returns the formatted string.

double num = 1234.56789;
Locale locale = Locale.US;
NumberFormat nf = NumberFormat.getCurrencyInstance(locale);
String formattedNum = NumberUtils.format(num, locale, nf); // Returns $1,234.57

Conversion

Converting between different number types can be a hassle, but Apache Commons Lang makes it a breeze with the NumberUtils class. This class provides a variety of methods for converting numbers between different types, including toInt(), toLong(), and toDouble().

int i = NumberUtils.toInt("42"); // Returns 42
long l = NumberUtils.toLong("123456789"); // Returns 123456789
double d = NumberUtils.toDouble("3.14"); // Returns 3.14

Comparison

Comparing numbers can be a bit tricky due to issues with floating point precision. Apache Commons Lang provides a variety of methods for comparing numbers that take these issues into account. Some of the most commonly used comparison methods include isEqual(), compare(), and min()/max().

boolean equal = NumberUtils.isEqual(3.14, 3.140000000000001); // Returns true
int cmp = NumberUtils.compare(42, 1337); // Returns -1 (42 is less than 1337)
double min = NumberUtils.min(1.0, 2.0, 3.0); // Returns 1.0
double max = NumberUtils.max(1.0, 2.0, 3.0); // Returns 3.0

And there you have it, me hearty! These are just a few examples of the many number manipulation methods available in Apache Commons Lang. With these tools at your disposal, you’ll be able to handle even the most challenging numerical problems in your code.

NumberUtils class

In addition to the individual number manipulation methods we just covered, Apache Commons Lang also provides a NumberUtils class that contains a wealth of useful number manipulation utilities.

Some of the most commonly used methods in the NumberUtils class include:

isNumber()

The isNumber() method checks whether a given String can be converted to a number. This can be useful when you need to validate user input or parse data from an external source.

boolean isNum = NumberUtils.isNumber("42"); // Returns true

createNumber()

The createNumber() method attempts to create a Number object from a given String. This method can handle a wide variety of number formats, including hexadecimals and scientific notation.

Number num = NumberUtils.createNumber("0xFF"); // Returns 255

toIntValue(), toLongValue(), toFloatValue(), toDoubleValue()

These methods convert a Number object to the corresponding primitive type.

Number num = Double.valueOf("3.14");
int i = NumberUtils.toIntValue(num); // Returns 3
long l = NumberUtils.toLongValue(num); // Returns 3
float f = NumberUtils.toFloatValue(num); // Returns 3.14f
double d = NumberUtils.toDoubleValue(num); // Returns 3.14

max(), min()

These methods find the maximum or minimum value in a collection of numbers.

List<Integer> nums = Arrays.asList(1, 2, 3, 4, 5);
int max = NumberUtils.max(nums); // Returns 5
int min = NumberUtils.min(nums); // Returns 1

compare()

The compare() method compares two Number objects and returns the result as an int. This method takes into account floating point precision issues.

int cmp = NumberUtils.compare(Double.valueOf("3.14"), Double.valueOf("3.14159")); // Returns -1

As you can see, the NumberUtils class provides a wide variety of useful number manipulation utilities. By incorporating these methods into your code, you can simplify your numerical tasks and handle edge cases more easily.

Keep in mind that the examples we’ve covered here are just a small sample of what’s available in the NumberUtils class. Be sure to check out the official documentation for a full list of available methods and their usage.

Common use cases for number operations

Now that we’ve covered some of the key number manipulation methods and the NumberUtils class in Apache Commons Lang, let’s take a look at some common use cases where these tools can come in handy.

Parsing user input

When you’re working with user input, you never know what kind of data you’re going to get. That’s why it’s important to be able to parse a wide variety of number formats. The NumberUtils.createNumber() method is perfect for this task, as it can handle everything from integers to scientific notation.

Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
String input = scanner.nextLine();
Number num = NumberUtils.createNumber(input);

Converting between number types

Sometimes you need to convert a number from one type to another. The NumberUtils class provides a variety of methods for converting between different types, such as toIntValue() and toDoubleValue(). These methods can come in handy when you need to perform calculations using numbers of different types.

float f = 3.14f;
int i = NumberUtils.toIntValue(f); // Returns 3
double d = NumberUtils.toDoubleValue(i); // Returns 3.0

Comparing numbers

Comparing numbers can be tricky due to issues with floating point precision. The NumberUtils.compare() method takes these issues into account, making it a safer alternative to the standard compareTo() method.

double num1 = 3.14;
double num2 = 3.1400000001;
int cmp = NumberUtils.compare(num1, num2); // Returns -1

Finding maximum and minimum values

When you’re working with collections of numbers, you might need to find the maximum or minimum value. The NumberUtils.max() and NumberUtils.min() methods are perfect for this task, and can handle collections of any size.

List<Integer> nums = Arrays.asList(1, 2, 3, 4, 5);
int max = NumberUtils.max(nums); // Returns 5
int min = NumberUtils.min(nums); // Returns 1

As you can see, there are many use cases where the number manipulation methods and NumberUtils class in Apache Commons Lang can come in handy. Whether you’re parsing user input, converting between number types, comparing numbers, or finding maximum and minimum values, Apache Commons Lang has got you covered.

So, set your sights on the horizon and continue your journey through the world of Apache Commons Lang. With the tools and techniques you’ve learned here today, you’re well on your way to becoming a master pirate programmer. Arrr!