Subversion Repository Public Repository

litesoft

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
// This Source Code is in the Public Domain per: http://litesoft.org/License.txt
package org.litesoft.core.simpletypes.nonpublic;

import org.litesoft.commonfoundation.typeutils.*;
import org.litesoft.core.simpletypes.*;

public class DecimalToStringParts
{
    private static final String PLUS = "+";
    private static final String MINUS = "-";

    private String mNegative = "";
    private String mWholePart;
    private String mFractionPart = "";

    public DecimalToStringParts( long pValue )
    {
        this( pValue, 0 );
    }

    public DecimalToStringParts( long pValue, int pDecimalPlaces )
    {
        if ( pValue < 0 )
        {
            mNegative = MINUS;
            pValue = -pValue;
        }
        String vStr = Long.toString( pValue );
        if ( pDecimalPlaces <= 0 )
        {
            mWholePart = vStr;
        }
        else
        {
            while ( vStr.length() <= pDecimalPlaces )
            {
                vStr = "0" + vStr;
            }
            int zDPat = vStr.length() - pDecimalPlaces;
            mWholePart = vStr.substring( 0, zDPat );
            mFractionPart = vStr.substring( zDPat );
        }
    }

    public DecimalToStringParts( String pValue, char pDecimalSeparator )
            throws IllegalArgumentException
    {
        String str = Strings.deNull( pValue ).trim();
        if ( str.startsWith( PLUS ) )
        {
            str = validate( str, PLUS, str.substring( 1 ), "started" );
        }
        else if ( str.startsWith( MINUS ) )
        {
            str = validate( str, mNegative = MINUS, str.substring( 1 ), "started" );
        }
        else if ( str.endsWith( PLUS ) )
        {
            str = validate( str, PLUS, str.substring( 0, str.length() - 1 ), "ended" );
        }
        else if ( str.endsWith( MINUS ) )
        {
            str = validate( str, mNegative = MINUS, str.substring( 0, str.length() - 1 ), "ended" );
        }
        StringBuilder sb = new StringBuilder();
        int zDPcnt = 0;
        for ( int i = 0; i < str.length(); i++ )
        {
            char c = str.charAt( i );
            if ( Character.isDigit( c ) )
            {
                sb.append( c );
            }
            else if ( c == pDecimalSeparator )
            {
                sb.append( c );
                if ( ++zDPcnt > 1 )
                {
                    throw new IllegalArgumentException( "2 '" + pDecimalSeparator + "' found in: " + pValue );
                }
            }
            else if ( Character.isLetter( c ) )
            {
                throw new IllegalArgumentException( "Unable to parse '" + c + "' found in: " + pValue );
            }
            // Ignore everything else
        }
        if ( zDPcnt == 0 )
        {
            sb.append( pDecimalSeparator );
        }
        str = sb.toString();
        int zDPat = str.indexOf( pDecimalSeparator );
        mWholePart = str.substring( 0, zDPat );
        mFractionPart = str.substring( zDPat + 1 );
        if ( mWholePart.length() == 0 )
        {
            if ( mFractionPart.length() == 0 )
            {
                throw new IllegalArgumentException( "Unable to parse, No digits found in: " + pValue );
            }
            mWholePart = "0";
        }
    }

    public DecimalToStringParts forceDecimalPlaces( Integer pDecimalPlaces, boolean pSilentlyTruncate )
            throws IllegalArgumentException
    {
        if ( pDecimalPlaces != null )
        {
            int zDPdesired = Math.abs( pDecimalPlaces.intValue() );
            if ( zDPdesired > 20 )
            {
                zDPdesired = 20;
            }
            while ( mFractionPart.length() < zDPdesired )
            {
                mFractionPart += "0";
            }
            int i = mFractionPart.length();
            if ( i > zDPdesired )
            {
                if ( !pSilentlyTruncate )
                {
                    for (; i > zDPdesired; i-- )
                    {
                        if ( mFractionPart.charAt( i - 1 ) != '0' )
                        {
                            throw new IllegalArgumentException( "Unable to reduce decimal places to " + zDPdesired + " places, precision lost" );
                        }
                    }
                }
                mFractionPart = (zDPdesired == 0) ? "" : mFractionPart.substring( 0, zDPdesired );
            }
        }
        return this;
    }

    public String getNegative()
    {
        return mNegative;
    }

    public String getWholePart()
    {
        return mWholePart;
    }

    public String getFractionPart()
    {
        return mFractionPart;
    }

    public String toString()
    {
        return (mFractionPart.length() == 0) ? //
               mNegative + mWholePart : //
               mNegative + mWholePart + "." + mFractionPart;
    }

    public String toString( NumericFormatControl pFormatControl )
    {
        if ( pFormatControl == null )
        {
            return toString();
        }
        String zWholePart = mWholePart;
        String zFractionPart = mFractionPart;
        char zThousandsSeparator = pFormatControl.getThousandsSeparator();
        if ( zThousandsSeparator != 0 )
        {
            zWholePart = injectFromEnd( zWholePart, zThousandsSeparator );
            if ( pFormatControl.isSeparateFractionalThousands() )
            {
                zFractionPart = injectFromStart( zFractionPart, zThousandsSeparator );
            }
        }
        return (zFractionPart.length() == 0) ? //
               mNegative + zWholePart : //
               mNegative + zWholePart + pFormatControl.getDecimalSeparator() + zFractionPart;
    }

    private String injectFromStart( String pPart, char pThousandsSeparator )
    {
        if ( pPart.length() <= 3 )
        {
            return pPart;
        }
        StringBuilder sb = new StringBuilder( pPart );
        for ( int i = 3; i < sb.length(); i += 3 )
        {
            sb.insert( i++, pThousandsSeparator );
        }
        return sb.toString();
    }

    private String injectFromEnd( String pPart, char pThousandsSeparator )
    {
        if ( pPart.length() <= 3 )
        {
            return pPart;
        }
        StringBuilder sb = new StringBuilder( pPart );
        for ( int i = sb.length() - 3; i > 0; i -= 3 )
        {
            sb.insert( i, pThousandsSeparator );
        }
        return sb.toString();
    }

    private String validate( String pOrig, String pChar, String pData, String pWhy )
    {
        String zWhyPlus;
        if ( pData.indexOf( pChar ) != -1 )
        {
            zWhyPlus = "an additional '" + pChar + "'";
        }
        else
        {
            String zOther = PLUS.equals( pChar ) ? MINUS : PLUS;
            if ( pData.indexOf( zOther ) == -1 )
            {
                return pData.trim();
            }
            zWhyPlus = "a '" + zOther + "'";
        }
        throw new IllegalArgumentException( "Unable to parse \"" + pOrig + "\", it " + pWhy + " with '" + pChar + "' and contained " + zWhyPlus );
    }
}

Commits for litesoft/trunk/Java/core/Anywhere/src/org/litesoft/core/simpletypes/nonpublic/DecimalToStringParts.java

Diff revisions: vs.
Revision Author Commited Message
939 Diff Diff GeorgeS picture GeorgeS Mon 02 Jun, 2014 21:30:31 +0000

Extracting commonfoundation

802 Diff Diff GeorgeS picture GeorgeS Wed 15 Aug, 2012 04:04:47 +0000
151 Diff Diff GeorgeS picture GeorgeS Thu 17 Mar, 2011 04:16:22 +0000
49 Diff Diff GeorgeS picture GeorgeS Mon 12 Apr, 2010 02:59:10 +0000

License Text

2 GeorgeS picture GeorgeS Sun 07 Feb, 2010 12:50:58 +0000