Back to index

wims  3.65+svn20090927
NumberUtils.java
Go to the documentation of this file.
00001 /*
00002 $Id: NumberUtils.java,v 1.2 2003/02/18 11:48:48 sander Exp $
00003 */
00004 
00005 
00006 package fr.ove.utils;
00007 
00008 import java.text.NumberFormat;
00009 
00015 public class NumberUtils {
00024     public static boolean isNumber(String token) {
00025         char oneChar;
00026         
00027         for (int i = 0; i < token.length(); i++) {
00028             oneChar = token.charAt(i);
00029             
00030             if (!Character.isDigit(oneChar) && (oneChar != '.'))
00031                 return false;
00032         }
00033         return true;
00034     }
00035     
00043     public static boolean isFloat(String token) {
00044         for (int i = 0; i < token.length(); i++)
00045             if (token.charAt(i) == '.')
00046                 return true;
00047                 
00048         return false;
00049     }
00050     
00055     public static String formatDouble(String dValue) {
00056         NumberFormat form;
00057         form = NumberFormat.getInstance();//get local number format instance
00058         form.setMaximumFractionDigits(2);//set for 2 digits only after decimal place
00059         form.setMinimumFractionDigits(1);//set for 2 digits after decimal place
00060         form.setGroupingUsed(false);//no comma for thousands
00061         form.setMinimumIntegerDigits(1);//leading 0 if < 1
00062 
00063         try {
00064             return form.format((Double.valueOf(dValue)).doubleValue());
00065         } catch (Exception execp) {
00066              return dValue;
00067         }
00068     }
00069 }