Skip to content

Commit 2da9d8e

Browse files
committed
closes #23
1 parent caf2cbd commit 2da9d8e

File tree

4 files changed

+56
-1
lines changed

4 files changed

+56
-1
lines changed

src/main/java/com/epam/parso/DataWriterUtil.java

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import java.io.IOException;
2323
import java.math.BigDecimal;
24+
import java.text.DecimalFormat;
2425
import java.text.SimpleDateFormat;
2526
import java.util.*;
2627

@@ -60,7 +61,7 @@ public final class DataWriterUtil {
6061
private static final String HOURS_OUTPUT_FORMAT = "%02d";
6162

6263
/**
63-
* The format to output minutes in the the CSV format.
64+
* The format to output minutes in the CSV format.
6465
*/
6566
private static final String MINUTES_OUTPUT_FORMAT = "%02d";
6667

@@ -81,6 +82,13 @@ public final class DataWriterUtil {
8182
*/
8283
private static final List<String> TIME_FORMAT_STRINGS = Arrays.asList("TIME", "HHMM");
8384

85+
/**
86+
* The format to store the percentage values. Appear in the data of
87+
* the {@link com.epam.parso.impl.SasFileParser.FormatAndLabelSubheader} subheader
88+
* and are stored in {@link Column#format}.
89+
*/
90+
private static final String PERCENT_FORMAT = "PERCENT";
91+
8492
/**
8593
* The number of seconds in a minute.
8694
*/
@@ -96,6 +104,12 @@ public final class DataWriterUtil {
96104
*/
97105
private static final Locale DEFAULT_LOCALE = Locale.getDefault();
98106

107+
/**
108+
* The format to output percentage values in the CSV format. This format is used
109+
* when {@link ColumnFormat#precision} does not contain the accuracy of rounding.
110+
*/
111+
private static final DecimalFormat ZERO_PRECISION_FORMAT = new DecimalFormat("0%");
112+
99113
/**
100114
* These are sas7bdat format references to {@link java.text.SimpleDateFormat} date formats.
101115
* <p>
@@ -204,6 +218,8 @@ private static String processEntry(Column column, Object entry, Locale locale) t
204218
} else {
205219
if (TIME_FORMAT_STRINGS.contains(column.getFormat().getName())) {
206220
valueToPrint = convertTimeElementToString((Long) entry);
221+
} else if (PERCENT_FORMAT.equals(column.getFormat().getName())) {
222+
valueToPrint = convertPercentElementToString(entry, column.getFormat());
207223
} else {
208224
valueToPrint = String.valueOf(entry);
209225
if (entry.getClass() == Double.class) {
@@ -274,6 +290,24 @@ private static String convertDoubleElementToString(Double value) {
274290
return valueToPrint;
275291
}
276292

293+
/**
294+
* The function to convert a percent element into a string. The accuracy of rounding
295+
* is stored in {@link Column#format}.
296+
* @param value the input numeric value to convert.
297+
* @param columnFormat the column format containing the precision of rounding the converted value.
298+
* @return the string with the text presentation of the input numeric value.
299+
*/
300+
private static String convertPercentElementToString(Object value, ColumnFormat columnFormat) {
301+
Double doubleValue = value instanceof Long ? ((Long) value).doubleValue() : (Double) value;
302+
DecimalFormat df = ZERO_PRECISION_FORMAT;
303+
int precision = columnFormat.getPrecision();
304+
if (precision != 0) {
305+
String pattern = "0%." + new String(new char[precision]).replace("\0", "0");
306+
df = new DecimalFormat(pattern);
307+
}
308+
return df.format(doubleValue);
309+
}
310+
277311
/**
278312
* The function to remove trailing zeros from the decimal part of the numerals represented by a string.
279313
* If there are no digits after the point, the point is deleted as well.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
recnum,square,desc,pctdone,pctincr,date,datetime,time
2+
0,0,This is row 0 of 10,0%,,01-01-1960,01Jan1960:00:00:01.00,00:00:01
3+
1,1,This is row 1 of 10,10%,0.0%,02-01-1960,01Jan1960:00:00:10.00,00:00:03
4+
2,4,This is row 2 of 10,20%,50.0%,17-01-1960,01Jan1960:00:01:40.00,00:00:09
5+
3,9,This is row 3 of 10,30%,66.7%,22-03-1960,01Jan1960:00:16:40.00,00:00:27
6+
4,16,This is row 4 of 10,40%,75.0%,13-09-1960,01Jan1960:02:46:40.00,00:01:21
7+
5,25,This is row 5 of 10,50%,80.0%,17-09-1961,02Jan1960:03:46:40.00,00:04:03
8+
6,36,This is row 6 of 10,60%,83.3%,20-07-1963,12Jan1960:13:46:40.00,00:12:09
9+
7,49,This is row 7 of 10,70%,85.7%,29-07-1966,25Apr1960:17:46:40.00,00:36:27
10+
8,64,This is row 8 of 10,80%,87.5%,20-03-1971,03Mar1963:09:46:40.00,01:49:21
11+
9,81,This is row 9 of 10,90%,88.9%,18-12-1977,09Sep1991:01:46:40.00,05:28:03
12+
10,100,This is row 10 of 10,100%,90.0%,19-05-1987,19Nov2276:17:46:40.00,16:24:09
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Number,Name,Type,Data length,Format,Label
2+
1,recnum,Numeric,8,,Record Number
3+
2,square,Numeric,8,,Square of the Record Number
4+
3,desc,Character,200,,Description of the Row
5+
4,pctdone,Numeric,8,PERCENT8.,Percent Done
6+
5,pctincr,Numeric,8,PERCENT7.1,Percent Increment
7+
6,date,Numeric,8,DDMMYYD10.,
8+
7,datetime,Numeric,8,DATETIME.,
9+
8,time,Numeric,8,TIME.,
128 KB
Binary file not shown.

0 commit comments

Comments
 (0)