PHP floatval

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

PHP : floatval() function

Description

The floatval() function is used to convert a value to a float.

Version

(PHP 4 and above)

Syntax

floatval (var1)

Parameter

Name

Description

Required /
Optional

Type

var1

May be any scalar type. You cannot use floatval() on arrays or objects.

Required

Mixed*

*Mixed : Mixed indicates that a parameter may accept multiple (but not necessarily all) types.

Return value

The float value of the given variable.

Value Type : Float.

Example :

  1. ?php  

  2. $var_name1="122.00,50";  

  3. $var_name2="122.00";  

  4. $var_name3="122.50";  

  5. $var_name4="122,50";  

  6. $var_name5="122 50";  

  7. $var_name6="0.50";  

  8. $var_name7="0,50";  

  9. $var_name8="-122.50";  

  10. $var_name9="-122,50";  

  11. $var_name10="-122 50";  

  12. echo floatval($var_name1)."
    ";  

  13. echo floatval($var_name2)."
    ";  

  14. echo floatval($var_name3)."
    ";  

  15. echo floatval($var_name4)."
    ";  

  16. echo floatval($var_name5)."
    ";  

  17. echo floatval($var_name6)."
    ";  

  18. echo floatval($var_name7)."
    ";  

  19. echo floatval($var_name8)."
    ";  

  20. echo floatval($var_name9)."
    ";  

  21. echo floatval($var_name10)."
    ";  

  22. ?>  

Output :

122
122
122.5
122
122
0.5
0
-122.5
-122
-122