Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 package org.jruby.util;
 
A safer way to parse float values

Prevents brute force attacks using the famous Java bug.

 
public final class SafeFloatParser extends SafeDecimalParser {

    
Safe way of parsing a Float value from a String

Parameters:
s The input String
Returns:
the Float value
    public static Float valueOf(String s) {
        Float result = null;
        Double decimalValue = decimalValueOf(s);
        if (decimalValue != null) {
            result = Float.valueOf(decimalValue.floatValue());
        }
        return result;
    }

    
Safe way of parsing a Float value from a String

Parameters:
s The input String
Returns:
the Float value
    public static Float parseFloat(String s) {
        return valueOf(s);
    }

    
Safe way of getting the float value
prevents BigDecimal from calling Float.parseFloat()

Parameters:
number
Returns:
the float value
    public static float floatValue(Number number) {
        return Float.valueOf((float)decimalValue(number));
    }

    
Safe way of getting the float value
Prevents BigDecimal from calling Float.parseFloat()

Parameters:
bigDecimal
Returns:
the float value
    public static float floatValue(BigDecimal bigDecimal) {
        return Float.valueOf((float)decimalValue(bigDecimal));
    }
New to GrepCode? Check out our FAQ X