1

I have to truncate the values obtained from database upto two fractional parts and put that values in the resultset I'm adding values from resultset into list in following way

  while(rs.next()) 
             {
                Comp_Mps_vs ref_drop=new Comp_Mps_vs();

                ref_drop.setLogtime(rs.getString(1));
                ref_drop.setBeam_current(rs.getString(2));
                ref_drop.setBeam_energy(rs.getString(3));
                ref_drop.setP99_readback(rs.getString(4));
                ref_drop.setP99_setvalue(rs.getString(5));
                ref_drop.setP99_vmeset(rs.getString(6));
                ref_jsp.add(ref_drop);
             }   

Where ref_jsp is

List<Comp_Mps_vs> ref_jsp=new ArrayList<Comp_Mps_vs>();

Now I wnat a method whcich round off the values upto two floating points and pass those index values in the ref_drop.setP99_vmeset(rs.getString(index_no)); code .

How to achieve this in java?

Cœur
  • 37,241
  • 25
  • 195
  • 267
tiddi rastogi
  • 526
  • 3
  • 10
  • 33
  • do you want to truncate the values on the `String` or on the value? Do you need to round up/down the values? Can you pls provide some examples for the desirable truncation? – JohnnyAW Mar 19 '15 at 13:21
  • @JohnnyAW If I want to truncate on String ,how to do??eg 123.4567889 to be truncated to 123.45. – tiddi rastogi Mar 20 '15 at 04:26

2 Answers2

0

Do you mean a number formatting like this?

Double doubleValue = Double.valueOf(rs.getString(6));
String formattedValue = String.format("%.2f", doubleValue);
ref_drop.setP99_vmeset(formattedValue);

so if you get 11.1111 String from the DB it'll be formatted to 11.11

S. Pauk
  • 5,208
  • 4
  • 31
  • 41
  • I have 162 columns ,then I have to write these 3 lines 162 times.Is there any other alternative to do it?? – tiddi rastogi Mar 20 '15 at 04:15
  • How to apply the for loop such that for every next rs.getString(i),the loop increments by 1.the for loop which may be applied is for(int i=1;i<=162;i++) { Double doubleValue = Double.valueOf(rs.getString(i)); String formattedValue = String.format("%.2f", doubleValue); – tiddi rastogi Mar 20 '15 at 04:19
  • you will anyways have 162 lines at some point cause you have to set your properties – S. Pauk Mar 20 '15 at 05:09
  • yes,I will ,but through this I will have to add 162+162 more lines ,so I want to apply a for loop on it,so that rs.getstring value get incremented each time.But can't get how to set it. – tiddi rastogi Mar 20 '15 at 05:38
0

In order to round up the value, first you need to convert it to a number. For this you can use

double d = Double.parseDouble(rs.getString(6))

For more details on this see here.

Then you'll need to create a formatted string:

String s  = new DecimalFormat(#.##").format(d);

After that you can assign it:

ref_drop.setP99_vmeset(s);
Community
  • 1
  • 1
jny
  • 8,007
  • 3
  • 37
  • 56
  • in this also I ahve the same issue that if I have such 162 set methods ,then 162 times this three lines with different setter method shave to be written.How to use for loop in that ,so that for every next rs.getString(),one incremented values can be suplied as integer.? – tiddi rastogi Mar 20 '15 at 04:48