What is NumberUtils.toFloat() in Java? – Queslers

What is NumberUtils.toFloat() in Java? Answer

Overview

toFloat() is a static method of the NumberUtils class that is used to convert the given string to a float value.

One variant of the method accepts a default value which is returned if the conversion fails.

How to import NumberUtils

The definition of NumberUtils can be found in the Apache Commons Lang package, which we can add to the Maven project by adding the following dependency to the pom.xml file:


<dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.12.0</version>
</dependency>

For other versions of the commons-lang package, refer to the Maven Repository.

You can import the NumberUtils class as follows:


import org.apache.commons.lang3.math.NumberUtils;

Syntax


public static float toFloat(String str, float defaultValue)

Parameters

  • String str: This is the string to convert.
  • float defaultValue: This is the default value to return.

Return value

This method returns a float value. If the conversion fails, it returns the default value.

Overloaded methods

public static float toFloat(final String str)

This variant of the function returns 0.0f if conversion fails.

Code

import org.apache.commons.lang3.math.NumberUtils;

public class Main{

    public static void main(String[] args){
        // Example 1
        String stringToConvert = "23.356";
        float defaultValue = 24.34f;
        System.out.printf("The output of the method NumberUtils.toFloat(%s, %s) is %s", stringToConvert, defaultValue, NumberUtils.toFloat(stringToConvert, defaultValue));
        System.out.println();

        // Example 2
        stringToConvert = "233sdf";
        defaultValue = 24.34f;
        System.out.printf("The output of the method NumberUtils.toFloat(%s, %s) is %s", stringToConvert, defaultValue, NumberUtils.toFloat(stringToConvert, defaultValue));
        System.out.println();

    }
}

Example 1

  • string to convert = 23.356
  • default value = 24.34f

The method returns 23.356 because the conversion is successful.

Example 2

  • string to convert = 233sdf
  • default value = 24.34f

The method returns 24.34 because the conversion is unsuccessful.

Expected output

The output of the code is as follows:


The output of the method NumberUtils.toFloat(23.356, 24.34) is 23.356
The output of the method NumberUtils.toFloat(233sdf, 24.34) is 24.34
What is NumberUtils.toFloat() in Java? Review:

In our experience, we suggest you solve this What is NumberUtils.toFloat() in Java? and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the What is NumberUtils.toFloat() in Java?

Find on Educative

Conclusion:

I hope this What is NumberUtils.toFloat() in Java? would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions

Leave a Reply

Your email address will not be published.