torres

Distinguished
Feb 2, 2010
7
0
18,510
Hi i want to display a set of data and such as doubles but the zero`s after the decimal is messing everything up.How can i display this using system.out.println or printf without rounding of the doubles. . I am using System.out.printf. Heres the code


void SchoolArrays(){
String Schools[] = {"Sutherland: ","Waterkloof: ","GlenhighSc: ","Riverhighc: ","Soweto high "};
double Metal[] = {56.5,32.4,103.2,46.7,82.3};
double Glass[] = {12.2,67.9,35.6,72.2,89.9};
double Paper[] = {95,19.1,9.3,53.6,29.1};
double Other[] = {32.4,43.2,0.5,39.4,13.2};
//Question 9
for(int x = 0;x<Metal.length;x++){
OverAllWeights = OverAllWeights + Metal[x] + Glass[x] + Paper[x] + Other[x];
}
System.out.printf("%s %10s %5s %5s %5s %5s","SchoolName","Metal","Glass","Paper","Other","Totals");
for(int x = 0;x<Schools.length;x++){
Total[x] = Metal[x] + Glass[x] + Paper[x] + Other[x];
System.out.printf("%s %5.0f %5.0f %5.0f %5.0f %8.0f","\n"+Schools[x],Metal[x],Glass[x],Paper[x],Other[x],Total[x]);
}
for(int x = 0;x<Schools.length;x++){
metal = metal + Metal[x];
glass = glass + Glass[x];
paper = paper + Paper[x];
other = other+Other[x];
}
System.out.printf("%s %11.0f %5.0f %5.0f %5.0f %7.0f","\n"+"Totals:",metal,glass,paper,other,OverAllWeights);
}
}


And The output
SchoolName Metal Glass Paper Other Totals
Sutherland: 57 12 95 32 196
Waterkloof: 32 68 19 43 163
GlenhighSc: 103 36 9 1 149
Riverhighc: 47 72 54 39 212
Soweto high 82 90 29 13 214
Totals: 321 278 206 129 934


THanks
 

mgwildi

Honorable
Apr 20, 2012
8
0
10,510
in printf, when using the "f" specifier for your parameters, the digit following the "." specifies how many digits you want shown after the decimal point ...

try:
System.out.printf("%s %5.1f %5.1f %5.1f %5.1f %8.1f", ...)

ref: http://www.cplusplus.com/reference/clibrary/cstdio/printf/