HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Java / String / Java convert float to String – Format to N decimal points

Java convert float to String – Format to N decimal points

Learn to convert float value to String using Float.toString() and String.valueOf() methods. Learn to format float to string to n decimal points.

1. Java convert float to String

Use any of given two methods for converting a java float value to String.

1.1. Convert float to String – Float.toString(float f)

This method returns a string representation of the float value passed as argument.

float pi = 3.1415927f;
		
String piString = Float.toString(pi);

System.out.println(piString);

Program output:

3.1415927

1.2. Convert float to String – String.valueOf(float f)

The valueOf() method takes an float value as argument and return the equivalent string representation. The representation is exactly the one returned by the Float.toString() method.

String.valueOf() method definition
public static String valueOf(float f) 
{
    return Float.toString(f);
}
String.valueOf() example
float pi = 3.1415927f;
		
String piString = String.valueOf(pi);

System.out.println(piString);

Program output:

3.1415927

2. Java format float to String

Use NumberFormat.format(float) method to format float value to string in predefined format – such as set decimal places in formatted string.

For example, we can format float to 2 decimal points as in given example.

float pi = 3.1415927f;

NumberFormat formatter = new DecimalFormat("0.00");

String formmatedFloatValue = formatter.format(pi);

System.out.println( formmatedFloatValue );

System.out.println(formatter.format(1.1));

System.out.println(formatter.format(1.123));

Program output:

3.14
1.10
1.12

3. Java convert String to float

Use Float.parseFloat(string) method to convert float value to string.

String piString = "3.1415927";

float pi = Float.parseFloat(piString);

System.out.println(pi);

Program output:

3.1415927

In this article, we learned to get float value from string in Java. You may be interested in reading about the correct way to compare two floats.

Happy Learning !!

Was this post helpful?

Let us know if you liked the post. That’s the only way we can improve.
TwitterFacebookLinkedInRedditPocket

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Comments are closed on this article!

Search Tutorials

String methods

  • String concat()
  • String hashCode()
  • String contains()
  • String compareTo()
  • String compareToIgnoreCase()
  • String equals()
  • String equalsIgnoreCase()
  • String charAt()
  • String indexOf()
  • String lastIndexOf()
  • String intern()
  • String split()
  • String replace()
  • String replaceFirst()
  • String replaceAll()
  • String substring()
  • String startsWith()
  • String endsWith()
  • String toUpperCase()
  • String toLowerCase()

String examples

  • Convert String to int
  • Convert int to String
  • Convert String to long
  • Convert long to String
  • Convert CSV String to List
  • Java StackTrace to String
  • Convert float to String
  • String – Alignment
  • String – Immutable
  • String – StringJoiner
  • Java – Split string
  • String – Escape HTML
  • String – Unescape HTML
  • String – Convert to title case
  • String – Find duplicate words
  • String – Left pad a string
  • String – Right pad a string
  • String – Reverse recursively
  • String – Leading whitespaces
  • String – Trailing whitespaces
  • String – Remove whitespaces
  • String – Reverse words
  • String – Find duplicate characters
  • String – Check empty string
  • String – Get first 4 characters
  • String – Get last 4 characters
  • String – (123) 456-6789 pattern
  • String – Interview Questions

Java Tutorial

  • Java Introduction
  • Java Keywords
  • Java Flow Control
  • Java OOP
  • Java Inner Class
  • Java String
  • Java Enum
  • Java Collections
  • Java ArrayList
  • Java HashMap
  • Java Array
  • Java Sort
  • Java Clone
  • Java Date Time
  • Java Concurrency
  • Java Generics
  • Java Serialization
  • Java Input Output
  • Java New I/O
  • Java Exceptions
  • Java Annotations
  • Java Reflection
  • Java Garbage collection
  • Java JDBC
  • Java Security
  • Java Regex
  • Java Servlets
  • Java XML
  • Java Puzzles
  • Java Examples
  • Java Libraries
  • Java Resources
  • Java 14
  • Java 12
  • Java 11
  • Java 10
  • Java 9
  • Java 8
  • Java 7

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Sealed Classes and Interfaces