English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

How do i change a string buffer to a float?
i am writing a calculator program and when the user enters the numbers, i store them in a string buffer.
how do i change the string buffer to a float or double so i can add, subtract, multiply or divide them.
thanx for all your help in advance.

2007-03-21 01:45:40 · 2 answers · asked by Chustar Of Naija 2 in Computers & Internet Programming & Design

2 answers

You cannot convert directly from StringBuffer to float or double.
You need to convert a StringBuffer to String.Then you can convert to float or double as you wish.
It can be done as shown
StringBuffer s1=new StringBuffer("31.0");
float f=Float.parseFloat(s1.toString());
double d=Double.parseDouble(s1.toString());

Note that there is a possiblity of NumberFormatException if the string is not a number which can be convertd to float.

2007-03-21 02:27:48 · answer #1 · answered by Siva 3 · 0 0

You need the following three methods.

Please note that they all take as input a String!!

2007-03-21 08:56:02 · answer #2 · answered by AnalProgrammer 7 · 0 0

fedest.com, questions and answers