I was reading these questions:
- Format double to 2 decimal places with leading 0s
- How to round a number to n decimal places in Java
- Formatting numbers with same amount of padding
- How can I pad an integers with zeros on the left?
But My question is a combination of before, and is not completely answer!!!
Here my situation.
I want a Double Number, but the format is like this.
Suppose this: 97.89, 120.7891, 4505.9804, 1.789056, 3.4, 27.029188
Constant digit is six (integer and fractional are variable according to number):
I think I raised wrongly.
97.89 -> 0097.89, 120.7891 -> 120.789, 4505.9804 -> 4505.98, 1.789056 -> 1.78906, 3.4 -> 00003.4, 27.029188 -> 27.0292
Thinking it better.
97.89 -> 97.8900
120.7891 -> 120.789
4505.9804 -> 4505.98
1.789056 -> 1.78906
3.4 -> 3.4000
27.029188 -> 27.0292
The Zeros Pad must be in right Side after the ..
The range of double number is [1.1, 30000.0]...
Some code:
public static double truncVal(double Value, int trunc) {
double NewValue = 0.0;
if (Value != 0.0) {
double inv = 1 / Value;
int Exp = (int) Math.floor(Math.log10(inv));
double pow = Math.pow(10, Exp + trunc);
NewValue = Math.floor(Value * pow) / pow;
}
return NewValue;
}
Here my Example:
Frequency:8.175798915643707, truncVal:8.17579
Frequency:10.913382232281373, truncVal:10.9133
Frequency:14.567617547440307, truncVal:14.5676
Frequency:19.445436482630058, truncVal:19.4454
Frequency:25.956543598746574, truncVal:25.9565
Frequency:34.64782887210901, truncVal:34.6478
Frequency:46.2493028389543, truncVal:46.2493
Frequency:61.7354126570155, truncVal:61.7354
Frequency:82.4068892282175, truncVal:82.4068
Frequency:110.0, truncVal:110.0
Frequency:146.8323839587038, truncVal:146.832
Frequency:195.99771799087463, truncVal:195.997
Frequency:261.6255653005986, truncVal:261.625
Frequency:349.2282314330039, truncVal:349.228
Frequency:466.1637615180899, truncVal:466.163
Frequency:622.2539674441618, truncVal:622.253
Frequency:830.6093951598903, truncVal:830.609
Frequency:1108.7305239074883, truncVal:1108.73
Frequency:1479.9776908465376, truncVal:1479.97
Frequency:1975.533205024496, truncVal:1975.53
Frequency:2637.02045530296, truncVal:2637.02
Frequency:3520.0, truncVal:3520.0
Frequency:4698.63628667852, truncVal:4698.63
Frequency:6271.926975707989, truncVal:6271.92
Frequency:8372.018089619156, truncVal:8372.01
Frequency:11175.303405856126, truncVal:11175.3