In this section of java tutorial we will showing on how to convert a string value into float value. This is essential in transformation of one values to another. Let’s say for an instance we might we want to do calculations prior to displaying a value to end user and the input comes in from the console which is of course a String literal. This example java source code in converting a string to float value is essential.

Java Example: Convert String to float value

Float class can be found in java.lang package. This java class exposes a method that can convert a string to float and float to string. The parsefloat java method under Float class can be used in order to convert a string to float value.

package com.javatutorialhq.tutorial;

public class StringToFloat {

	/**
	 * This java sample code shows how to convert
	 * string variable into float type
	 * Property of javatutorialhq.com
	 * All Rights Reserved
	 * Version 1.0
	 * 05/22/2012
	 */
	public static void main(String[] args) {
		String salary = "500.50";
		float fSalary = Float.parseFloat(salary);
		float taxRate = 0.15f;
		float totalTax = fSalary * taxRate;
		System.out.println("Total Tax incurred = "+totalTax);
	}
}

Sample output is:

Total Tax incurred = 75.075005

This sample program computes the tax incurred based from a String value of salary and a taxrate in float. It is expected to covert first the String value of salary into float before multiplying it with taxRate which is float. The result value should be in float.