001 
002 /*
003  *  JScripter Simulation 1.0 - For Java To Script
004  *  Copyright (C) 2008-2011  J.J.Liu<jianjunliu@126.com> <http://www.jscripter.org>
005  *  
006  *  This program is free software: you can redistribute it and/or modify
007  *  it under the terms of the GNU Affero General Public License as published by
008  *  the Free Software Foundation, either version 3 of the License, or
009  *  (at your option) any later version.
010  *  
011  *  This program is distributed in the hope that it will be useful,
012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014  *  GNU Affero General Public License for more details.
015  *  
016  *  You should have received a copy of the GNU Affero General Public License
017  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
018  */
019 
020 package org.jscripter.sim;
021 
022 import java.math.RoundingMode;
023 import java.text.DecimalFormat;
024 
025 import js.NumberLike;
026 
027 final class SimNumberLike<T extends Number> implements NumberLike<T>
028 {
029     private final T n;
030 
031     public SimNumberLike(T n) {
032         this.n = n;
033     }
034 
035     private static final DecimalFormat getExponentialFormat(int digits) {
036         DecimalFormat df = new DecimalFormat("0.0E0");
037         df.setDecimalSeparatorAlwaysShown(false);
038         df.setMaximumFractionDigits(digits);
039         return df;
040     }
041 
042     //@Override
043     public final String toExponential() {
044         return getExponentialFormat(0).format(valueOf());
045     }
046 
047     //@Override
048     public final String toExponential(Object digits) {
049         return getExponentialFormat(SimUtil.getNumber(digits).intValue()).format(valueOf());
050     }
051 
052     private static final DecimalFormat getFixedFormat(int digits) {
053         DecimalFormat df = new DecimalFormat("0.0");
054         df.setDecimalSeparatorAlwaysShown(false);
055         df.setMaximumFractionDigits(digits);
056         df.setRoundingMode(RoundingMode.DOWN);
057         return df;
058     }
059 
060     //@Override
061     public final String toFixed() {
062         return getFixedFormat(0).format(valueOf());
063     }
064 
065     //@Override
066     public final String toFixed(Object digits) {
067         return getFixedFormat(SimUtil.getNumber(digits).intValue()).format(valueOf());
068     }
069 
070     //@Override
071     public final String toPrecision() {
072         return valueOf().toString();
073     }
074 
075     private static final DecimalFormat getPrecisionFormat(int digits) {
076         DecimalFormat df = new DecimalFormat("0.0E0");
077         df.setMinimumIntegerDigits(digits);
078         df.setMaximumFractionDigits(0);
079         df.setDecimalSeparatorAlwaysShown(false);
080         return df;
081     }
082 
083     //@Override
084     public final String toPrecision(Object precision) {
085         int p = SimUtil.getNumber(precision).intValue();
086         Number v = valueOf();
087         double d = Double.parseDouble(getPrecisionFormat(p).format(v));
088         if (v instanceof Double || v instanceof Float) {
089             return Double.toString(d);
090         } else {
091             return Long.toString((long)d);
092         }
093     }
094 
095     //@Override
096     public final String toLocaleString() {
097         return n.toString();
098     }
099 
100     //@Override
101     public final String toString() {
102         return n.toString();
103     }
104 
105     //@Override
106     public final String toString(Object radix) {
107         return n.toString();
108     }
109 
110     //@Override
111     public final String typeof() {
112         return "number";
113     }
114 
115     //@Override
116     public final boolean undefined() {
117         return false;
118     }
119 
120     //@Override
121     public final T valueOf() {
122         return n;
123     }
124 }