View Javadoc

1   package com.sharkysoft.printf;
2   
3   import java.math.BigDecimal;
4   import com.sharkysoft.math.MathToolbox;
5   import com.sharkysoft.printf.engine.NumberFormatter;
6   import com.sharkysoft.printf.engine.RealFormatter;
7   
8   abstract class Formatter_Real extends Formatter_Number
9   {
10  
11    protected Formatter_Real(final FormatSpecifier ipPfs)
12    {
13      super(ipPfs, new RealFormatter());
14      final RealFormatter rf = (RealFormatter) mpFormatter;
15      switch (ipPfs.mnPrecision)
16      {
17      case FormatSpecifier.PRECISION_UNSPECIFIED :
18        rf.setMinRightDigits(6);
19        rf.setMaxRightDigits(6);
20        break;
21      case FormatSpecifier.PRECISION_ARGUMENT :
22        break;
23      default :
24        rf.setMinRightDigits(ipPfs.mnPrecision);
25        rf.setMaxRightDigits(ipPfs.mnPrecision);
26      }
27      if (ipPfs.mcPositivePrefix != FormatSpecifier.PREFIX_UNSPECIFIED)
28      {
29        final NumberFormatter nf = (NumberFormatter) mpFormatter;
30        final String vsPrefix = String.valueOf(ipPfs.mcPositivePrefix);
31        nf.setZeroPrefix(vsPrefix);
32        nf.setPosPrefix(vsPrefix);
33      }
34      if (ipPfs.mcAlternate == FormatSpecifier.ALTERNATE_ON)
35      {
36        rf.setShowDecPoint(true);
37      }
38    }
39    
40    void adjustWidthAndPrecision(final PrintfState ipPs)
41    {
42      adjustWidth(ipPs);
43      adjustPrecision(ipPs);
44    }
45    
46    void adjustPrecision(final PrintfState ipPs)
47    {
48      if (mzVariablePrecision)
49      {
50        final RealFormatter vpRf = (RealFormatter) mpFormatter;
51        final int vnRightDigits = ((Number) ipPs.mapArgs[ipPs.mnArgIndex++]).intValue();
52        vpRf.setMinRightDigits(vnRightDigits);
53        vpRf.setMaxRightDigits(vnRightDigits);
54      }
55    }
56    
57    void formatFloat(final PrintfState ipPs)
58    {
59      ipPs.mpOutput.append
60      ( ( (RealFormatter) mpFormatter
61        ).format
62        ( ((Number) ipPs.mapArgs[ipPs.mnArgIndex++]).floatValue()
63        )
64      );
65    }
66  
67    void formatDouble(final PrintfState ipPs)
68    {
69      ipPs.mpOutput.append
70      ( ( (RealFormatter) mpFormatter
71        ).format
72        ( ((Number) ipPs.mapArgs[ipPs.mnArgIndex++]).doubleValue()
73        )
74      );
75    }
76    
77    void formatBigDecimal(final PrintfState ipPs)
78    {
79      ipPs.mpOutput.append
80      ( ( (RealFormatter) mpFormatter
81        ).format
82        ( (BigDecimal) ipPs.mapArgs[ipPs.mnArgIndex++]
83        )
84      );
85    }
86    
87    void formatFloatScientific(final PrintfState ipPs)
88    {
89      ipPs.mpOutput.append
90      ( ( (RealFormatter) mpFormatter
91        ).format
92        ( MathToolbox.toScientificNotation
93          ( ((Number) ipPs.mapArgs[ipPs.mnArgIndex++]).floatValue()
94          )
95        )
96      );
97    }
98    
99    void formatDoubleScientific(final PrintfState ipPs)
100   {
101     ipPs.mpOutput.append
102     ( ( (RealFormatter) mpFormatter
103       ).format
104       ( MathToolbox.toScientificNotation
105         ( ((Number) ipPs.mapArgs[ipPs.mnArgIndex++]).doubleValue()
106         )
107       )
108     );
109   }
110   
111   void formatBigDecimalScientific(final PrintfState ipPs)
112   {
113     ipPs.mpOutput.append
114     ( ( (RealFormatter) mpFormatter
115       ).format
116       ( MathToolbox.toScientificNotation
117         ( (BigDecimal) ipPs.mapArgs[ipPs.mnArgIndex++]
118         )
119       )
120     );
121   }
122 
123 }
124