All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.ioc.commons.impl.android.i18n.AndroidNumberFormatter Maven / Gradle / Ivy

package org.ioc.commons.impl.android.i18n;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.ParseException;

import org.ioc.commons.i18n.NumberFormatter;
import org.ioc.commons.utils.Format;

public class AndroidNumberFormatter implements NumberFormatter {

	@Override
	public String format(SystemDefaultFormat format, Number number) {
		switch (format) {
		case CurrencyFormat:
			return NumberFormat.getCurrencyInstance().format(number);
		case DecimalFormat:
			return new DecimalFormat().format(number);
		case PercentFormat:
			return NumberFormat.getPercentInstance().format(number);
		case ScientistFormat:
			return NumberFormat.getNumberInstance().format(number);
		}

		throw new IllegalArgumentException(Format.substitute("The {0} {1} is not recognized.",
				SystemDefaultFormat.class.getName(), format));
	}

	@Override
	public double parse(SystemDefaultFormat format, String stringNumber) {
		try {
			switch (format) {
			case CurrencyFormat:
				return NumberFormat.getCurrencyInstance().parse(stringNumber).doubleValue();
			case DecimalFormat:
				return new DecimalFormat().parse(stringNumber).doubleValue();
			case PercentFormat:
				return NumberFormat.getPercentInstance().parse(stringNumber).doubleValue();
			case ScientistFormat:
				return NumberFormat.getNumberInstance().parse(stringNumber).doubleValue();
			}
		} catch (ParseException e) {
			throw new RuntimeException(e);
		}

		throw new IllegalArgumentException(Format.substitute("The {0} {1} is not recognized.",
				SystemDefaultFormat.class.getName(), format));
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy