View Javadoc
1   package io.jawk.jrt;
2   
3   /*-
4    * ╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲
5    * Jawk
6    * ჻჻჻჻჻჻
7    * Copyright (C) 2006 - 2026 MetricsHub
8    * ჻჻჻჻჻჻
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU Lesser General Public License as
11   * published by the Free Software Foundation, either version 3 of the
12   * License, or (at your option) any later version.
13   *
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Lesser Public License for more details.
18   *
19   * You should have received a copy of the GNU General Lesser Public
20   * License along with this program.  If not, see
21   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
22   * ╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱
23   */
24  
25  import java.math.BigDecimal;
26  
27  final class StrNum {
28  
29  	private final String value;
30  	private final char decimalSeparator;
31  	private Boolean numeric;
32  	private Double numericValue;
33  
34  	StrNum(String value) {
35  		this(value, '.');
36  	}
37  
38  	StrNum(String value, char decimalSeparator) {
39  		this.value = value == null ? "" : value;
40  		this.decimalSeparator = decimalSeparator;
41  	}
42  
43  	boolean isNumber() {
44  		if (numeric == null) {
45  			numeric = Boolean.valueOf(JRT.isParseableNumber(value, decimalSeparator));
46  		}
47  		return numeric.booleanValue();
48  	}
49  
50  	double doubleValue() {
51  		if (numericValue == null) {
52  			numericValue = Double.valueOf(parseDoubleValue());
53  		}
54  		return numericValue.doubleValue();
55  	}
56  
57  	private double parseDoubleValue() {
58  		String normalizedValue = JRT.normalizeNumberForComparison(value, decimalSeparator);
59  		try {
60  			return Double.parseDouble(normalizedValue);
61  		} catch (NumberFormatException nfe) {
62  			return new BigDecimal(normalizedValue).doubleValue();
63  		}
64  	}
65  
66  	@Override
67  	public String toString() {
68  		return value;
69  	}
70  }