0

I have 4 number pickers and I concat as string the 4 numbers How I am going to convert this into long I using the following

String decimals=units2.concat(units3).concat(units4);
String units = units1;
String finalGrade = units1+decimals;
long Grade = Long.valueOf(finalGrade).longValue();
NumberFormat f = new DecimalFormat("000");
txtGrades.setText(f.format(Grade));

But I get instead of 1.234 a 1234

Miaoulis Nikos
  • 182
  • 1
  • 15

1 Answers1

0

Just do something like this:

String resultAsString = String.valueOf(units1) 
    + "." 
    + String.valueOf(units2) + 
    + String.valueOf(units3) + 
    + String.valueOf(units4);
Double result = Double.valueOf(resultAsString);

Or you could update this line in your code:

String finalGrade = units1+decimals;

To this:

String finalGrade = units1 + "." + decimals;

This can be further optimised with StringBuilder and such, but I wanted to present the general idea :-)

Also, from experience, doing stuff like this using Strings ensures you get frontal 0s right (which is important for stuff like numeric passwords), as using numerical calculations will usually result in the frontal 0s being tossed away.

Kelevandos
  • 7,024
  • 2
  • 29
  • 46