BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java String to float Conversion

By Chaitanya Singh | Filed Under: java

In this guide, we will see how to convert String to float in Java. We will use the parseFloat() method of Float class for this conversion.

public static float parseFloat(String str)

This method takes string argument and returns a float number represented by the argument str.

Program to Convert String to float

public class JavaExample{
  public static void main(String args[]){
    String str = "15.6515";
    float floatNum = Float.parseFloat(str);
    System.out.println("String to float: "+floatNum);
  }
}

Output:

Java String to float Example Output

Print float value upto two decimal places only:

In case, you want to display float number upto a certain number of decimal places then you can use the %.nf format specifier which tells the compiler to display number upto n decimal places only. Here we want to display only two decimal places so we used %.2f.

public class JavaExample{
  public static void main(String args[]){
    String str = "15.6515";
    float floatNum = Float.parseFloat(str);
    System.out.print("String to float: ");
    System.out.printf("%.2f", floatNum);
  }
}

Output:

Print float value upto two decimal places only

Program to Convert String with commas to float

Some countries use comma , instead of dot . to separate decimal parts of the number. In such scenario, we can use the valueOf() method of Float class. This method expects the string to contain dot instead of comma so we need to call replace() method of String class to replace the comma with dot.

public class JavaExample{
  public static void main(String args[]){
    String str = "15,6515";
    float floatNum = Float.valueOf(str.replace(",", ".").toString());
    System.out.println("String to float: "+floatNum);
  }
}

Output:

String to float: 15.6515

Recommended Posts

  • Java float to String Conversion
  • Java String to boolean Conversion
  • Java String to long Conversion
  • Java String to double Conversion
  • Java String to int Conversion
  • Java String to char Conversion
  • Java String to Date Conversion
  • Java Program to split the String by Comma

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap