All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.softicar.platform.common.core.number.parser.FloatParser Maven / Gradle / Ivy

Go to download

The SoftiCAR Platform is a lightweight, Java-based library to create interactive business web applications.

There is a newer version: 50.0.0
Show newest version
package com.softicar.platform.common.core.number.parser;

import com.softicar.platform.common.core.utils.DevNull;
import java.util.Optional;

/**
 * Utility methods for parsing {@link Float}.
 *
 * @author Oliver Richers
 */
public class FloatParser {

	/**
	 * Checks if the given string is a valid {@link Float} number.
	 * 

* If the given string is null, this method returns false. * * @param string * the string to parse (may be null) * @return true if the given string is a valid floating point literal */ public static boolean isFloat(String string) { return parse(string).isPresent(); } /** * Tries to parse the given string into a {@link Float}. *

* This method returns {@link Optional#empty()} if the given string cannot * be parsed into a valid floating-point value. *

* If the given string is null, this method also returns * {@link Optional#empty()}. * * @param string * the string to parse (may be null) * @return a {@link Optional} holding the parsed floating-point value or not */ public static Optional parse(String string) { if (string == null) { return Optional.empty(); } try { return Optional.of(Float.parseFloat(string)); } catch (NumberFormatException exception) { DevNull.swallow(exception); return Optional.empty(); } } public static Float parseFloat(String string) { return parse(string).orElse(null); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy