This is the fifth solution to find the reverse of a float value using the StringBuilder (mutable) data type.
import java.util.Scanner;
public class ReverseOfDecimal {
public static void main(String[] args) {
// TODO Auto-generated method stub
@SuppressWarnings("resource")
Scanner inpFloat= new Scanner(System.in);
//Input the float value
System.out.println("Enter a float value=");
float floatVal=inpFloat.nextFloat();
System.out.println("The float value is="+floatVal);
StringBuilder flt2Str=new StringBuilder(String.valueOf(floatVal));
String revFloatVal=flt2Str.reverse().toString();
System.out.println("The reverse value of decimal="+floatVal+" is="+Float.parseFloat(revFloatVal));
}
}
Output:
Enter a float value=
34.521
The float value is=34.521
The reverse value of decimal=34.521 is=125.43

Leave a comment