0

I was reading these questions:

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
Jim Mischel
  • 126,196
  • 18
  • 182
  • 330
QA_Col
  • 1,095
  • 9
  • 23
  • 3
    What about "123456789.00"? Would it display "123456"? And would "0.000000123" be displayed as "0.00000" or "00000.0"? – AntonH Nov 16 '17 at 20:48
  • That answers upper boundary, but not lower ... – AntonH Nov 16 '17 at 20:51
  • What you want to do is very specific and not common use-case, you'll have to implement it by yourself - there is no "magic tool/formula" that will solve it for you. – Nir Alfasi Nov 16 '17 at 20:52
  • you keep ignoring his second question: And would "0.000000123" be displayed as "0.00000" or "00000.0"? – Nir Alfasi Nov 16 '17 at 20:53
  • @QA_Col That is understood. I'm asking about a case where the number is very small, such as "0.000000123". How should it be displayed? – AntonH Nov 16 '17 at 20:53
  • @alfasin the right padding prevails over left padding, in this case `00000.0` – QA_Col Nov 16 '17 at 20:59
  • 1
    so why 1.789056 -> 1.78906 and not: 1.789056 -> 00001.78 ? – Nir Alfasi Nov 16 '17 at 21:02
  • @alfasin As I understand it, padding is to the left ***only if*** padding is required, as in, the String is less that 6 digits long (7 characters, if you include the .). I'm not 100% sure about "1.2345012" though, but I believe it would be "1.23450", and not "01.2345". To be confirmed by OP ... – AntonH Nov 16 '17 at 21:03
  • Start by adding more example with more extreme value (AntonH) suggest quite a few. From that, it could be a bit more clear – AxelH Nov 16 '17 at 21:13
  • @alfasin the right Zeros are padded later (after) the valuable (non zeros) fractional digits are taken in account. I moving on nonlinear double values, when the integer digits is low the fractional digits have more importance, but when the integer digits is high the fractional digits have less importance. – QA_Col Nov 16 '17 at 21:23
  • You can't do this *at all* in floating-point. Floating point doesn't *have* decimal places. It has binary places, and the two are incommensurable. You have to use a decimal radix, i.e. `BigDecimal` or some flavour of `DecimalFormat`. – user207421 Nov 16 '17 at 22:58
  • @alfasin Sorry by my mistake. – QA_Col Nov 16 '17 at 23:28

0 Answers0