Skip to Content

Convert Java String to Float Object Example

  • strict warning: Non-static method view::load() should not be called statically in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/views.module on line 906.
  • strict warning: Declaration of views_handler_argument::init() should be compatible with views_handler::init(&$view, $options) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/handlers/views_handler_argument.inc on line 0.
  • strict warning: Declaration of views_handler_filter::options_validate() should be compatible with views_handler::options_validate($form, &$form_state) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/handlers/views_handler_filter.inc on line 0.
  • strict warning: Declaration of views_handler_filter::options_submit() should be compatible with views_handler::options_submit($form, &$form_state) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/handlers/views_handler_filter.inc on line 0.
  • strict warning: Declaration of views_handler_filter_boolean_operator::value_validate() should be compatible with views_handler_filter::value_validate($form, &$form_state) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/handlers/views_handler_filter_boolean_operator.inc on line 0.
  • strict warning: Declaration of views_plugin_style_default::options() should be compatible with views_object::options() in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/plugins/views_plugin_style_default.inc on line 0.
  • strict warning: Declaration of views_plugin_row::options_validate() should be compatible with views_plugin::options_validate(&$form, &$form_state) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/plugins/views_plugin_row.inc on line 0.
  • strict warning: Declaration of views_plugin_row::options_submit() should be compatible with views_plugin::options_submit(&$form, &$form_state) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/plugins/views_plugin_row.inc on line 0.

  1. /*
  2.   Convert Java String to Float example
  3.   This example shows how we can convert String object to Float object.
  4. */
  5.  
  6. public class JavaStringToFloatExample {
  7.  
  8.   public static void main(String[] args) {
  9.    
  10.     //We can convert String to Float using one of the following ways.
  11.     //1. Construct Float using constructor.
  12.     Float fObj1 = new Float("100.564");
  13.     System.out.println(fObj1);
  14.    
  15.     //2. Use valueOf method of Float class. This method is static.
  16.     String str1 = "100.476";
  17.     Float fObj2 = Float.valueOf(str1);
  18.     System.out.println(fObj2);
  19.    
  20.     /*
  21.     To convert a String object to a float primitive value parseFloat method
  22.     of Float class. This is a static method.
  23.     */
  24.     String str2 = "76.39";
  25.     float f = Float.parseFloat(str2);
  26.     System.out.println(f);
  27.    
  28.     //Please note that these methods can throw a NumberFormatException if
  29.     //string can not be parsed.
  30.  
  31.   }
  32. }
  33.  
  34. /*
  35. Output of the program would be :
  36. 100.564
  37. 100.476
  38. 76.39
  39. */