Example usage for java.text NumberFormat getNumberInstance

List of usage examples for java.text NumberFormat getNumberInstance

Introduction

In this page you can find the example usage for java.text NumberFormat getNumberInstance.

Prototype

public static NumberFormat getNumberInstance(Locale inLocale) 

Source Link

Document

Returns a general-purpose number format for the specified locale.

Usage

From source file:easyJ.common.validate.GenericTypeValidator.java

/**
 * Checks if the value can safely be converted to an int primitive.
 * //w w  w.  java  2  s.  c  o m
 * @param value
 *                The value validation is being performed on.
 * @param locale
 *                The locale to use to parse the number (system default if
 *                null)
 * @return the converted Integer value.
 */
public static Integer formatInt(String value, Locale locale) {
    Integer result = null;

    if (value != null) {
        NumberFormat formatter = null;
        if (locale != null) {
            formatter = NumberFormat.getNumberInstance(locale);
        } else {
            formatter = NumberFormat.getNumberInstance(Locale.getDefault());
        }
        formatter.setParseIntegerOnly(true);
        ParsePosition pos = new ParsePosition(0);
        Number num = formatter.parse(value, pos);

        // If there was no error and we used the whole string
        if (pos.getErrorIndex() == -1 && pos.getIndex() == value.length()) {
            if (num.doubleValue() >= Integer.MIN_VALUE && num.doubleValue() <= Integer.MAX_VALUE) {
                result = new Integer(num.intValue());
            }
        }
    }

    return result;
}

From source file:org.lingcloud.molva.ocl.util.GenericTypeValidator.java

/**
 * Checks if the value can safely be converted to an int primitive.
 * /*from   w  w w.j  a v a  2s. com*/
 * @param value
 *            The value validation is being performed on.
 * @param locale
 *            The locale to use to parse the number (system default if null)
 * @return the converted Integer value.
 */
public static Integer formatInt(String value, Locale locale) {
    Integer result = null;

    if (value != null) {
        NumberFormat formatter = null;
        if (locale != null) {
            formatter = NumberFormat.getNumberInstance(locale);
        } else {
            formatter = NumberFormat.getNumberInstance(Locale.getDefault());
        }
        formatter.setParseIntegerOnly(true);
        ParsePosition pos = new ParsePosition(0);
        Number num = formatter.parse(value, pos);

        // If there was no error and we used the whole string
        if (pos.getErrorIndex() == -1 && pos.getIndex() == value.length()) {
            if (num.doubleValue() >= Integer.MIN_VALUE && num.doubleValue() <= Integer.MAX_VALUE) {
                result = Integer.valueOf(num.intValue());
            }
        }
    }

    return result;
}

From source file:name.gumartinm.weather.information.notification.NotificationIntentService.java

private void showNotification(final Current current, final WeatherLocation weatherLocation) {
    // 1. Update units of measurement.
    final UnitsConversor tempUnitsConversor = new TempUnitsConversor(this.getApplicationContext());

    // 2. Formatters
    final DecimalFormat tempFormatter = (DecimalFormat) NumberFormat.getNumberInstance(Locale.US);
    tempFormatter.applyPattern("###.##");

    // 3. Prepare data for RemoteViews.
    String tempMax = "";
    if (current.getMain().getTemp_max() != null) {
        double conversion = (Double) current.getMain().getTemp_max();
        conversion = tempUnitsConversor.doConversion(conversion);
        tempMax = tempFormatter.format(conversion) + tempUnitsConversor.getSymbol();
    }//ww  w .  j a va2s . com
    String tempMin = "";
    if (current.getMain().getTemp_min() != null) {
        double conversion = (Double) current.getMain().getTemp_min();
        conversion = tempUnitsConversor.doConversion(conversion);
        tempMin = tempFormatter.format(conversion) + tempUnitsConversor.getSymbol();
    }
    Bitmap picture;
    if ((current.getWeather().size() > 0) && (current.getWeather().get(0).getIcon() != null)
            && (IconsList.getIcon(current.getWeather().get(0).getIcon()) != null)) {
        final String icon = current.getWeather().get(0).getIcon();
        picture = BitmapFactory.decodeResource(this.getResources(),
                IconsList.getIcon(icon).getResourceDrawable());
    } else {
        picture = BitmapFactory.decodeResource(this.getResources(), R.drawable.weather_severe_alert);
    }
    final String city = weatherLocation.getCity();
    final String country = weatherLocation.getCountry();

    // 4. Insert data in RemoteViews.
    final RemoteViews remoteView = new RemoteViews(this.getApplicationContext().getPackageName(),
            R.layout.weather_notification);
    remoteView.setImageViewBitmap(R.id.weather_notification_image, picture);
    remoteView.setTextViewText(R.id.weather_notification_temperature_max, tempMax);
    remoteView.setTextViewText(R.id.weather_notification_temperature_min, tempMin);
    remoteView.setTextViewText(R.id.weather_notification_city, city);
    remoteView.setTextViewText(R.id.weather_notification_country, country);

    // 5. Activity launcher.
    final Intent resultIntent = new Intent(this.getApplicationContext(), MainTabsActivity.class);
    // The PendingIntent to launch our activity if the user selects this notification.
    // The stack builder object will contain an artificial back stack for the started Activity.
    // This ensures that navigating backward from the Activity leads out of
    // your application to the Home screen.
    final TaskStackBuilder stackBuilder = TaskStackBuilder.create(this.getApplicationContext());
    // Adds the back stack for the Intent (but not the Intent itself)
    stackBuilder.addParentStack(MainTabsActivity.class);
    // Adds the Intent that starts the Activity to the top of the stack
    stackBuilder.addNextIntent(resultIntent);
    final PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
            PendingIntent.FLAG_UPDATE_CURRENT);

    final NotificationManagerCompat notificationManager = NotificationManagerCompat
            .from(this.getApplicationContext());

    // 6. Create notification.
    final NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(
            this.getApplicationContext()).setContent(remoteView).setSmallIcon(R.drawable.ic_launcher)
                    .setAutoCancel(true).setLocalOnly(true).setWhen(System.currentTimeMillis())
                    .setContentIntent(resultPendingIntent).setPriority(NotificationCompat.PRIORITY_DEFAULT);

    final Notification notification = notificationBuilder.build();
    notification.flags |= Notification.FLAG_AUTO_CANCEL;

    // Send the notification.
    // Sets an ID for the notification, so it can be updated (just in case)
    int notifyID = 1;
    notificationManager.notify(notifyID, notification);
}

From source file:com.fluke.realtime.data.RediffParser.java

private int getInt(String val) {
    try {//from  w  w w  .  j av  a  2  s.  co m
        return NumberFormat.getNumberInstance(java.util.Locale.US).parse(val).intValue();
    } catch (ParseException ex) {
        throw new RuntimeException(ex);
    }
}

From source file:com.fluke.realtime.data.RediffParser.java

private float getFloat(String val) {
    try {/*from   w w  w.j  a  v a  2  s .c o  m*/
        return NumberFormat.getNumberInstance(java.util.Locale.US).parse(val).floatValue();
    } catch (ParseException ex) {
        throw new RuntimeException(ex);
    }
}

From source file:alignment.BinaryToCSV.java

/**
 * /*from  w  w  w . j a  va2  s  . c  o m*/
 *
 * @param 
 * @return 
 */
public int readHeader() {
    byte[] buffer = new byte[HEADER_SIZE]; //178
    int nRead = 0;

    try {
        if ((nRead = inputStream.read(buffer)) == -1)
            System.err.println("Header read failed");
    } catch (IOException e) {
        e.printStackTrace();
    }
    counter += nRead;

    byteToBits(buffer[0], 0);
    LN = b7;
    System.out.println("Low Noise: " + LN);
    gyro = b6;
    System.out.println("Gyroscope: " + gyro);
    mag = b5;
    System.out.println("Magnetometer: " + mag);

    byteToBits(buffer[1], 1);
    WR = b4;
    System.out.println("Wide Range: " + WR);

    byteToBits(buffer[10], 10);
    sync = b2;
    System.out.println("Sync: " + sync);
    master = b1;
    System.out.println("Master: " + master);

    NumberFormat nf = NumberFormat.getNumberInstance(Locale.ENGLISH);
    DecimalFormat df = (DecimalFormat) nf;
    df.applyPattern("#.#");
    sampleRate = Double.parseDouble(
            df.format(TWO_EXP_15 / (double) ((int) (buffer[21] & 0xFF) + ((int) (buffer[20] & 0xFF) << 8))));
    System.out.println("SampleRate: " + sampleRate);

    byte[] tempBytes = new byte[4];
    tempBytes[3] = buffer[174];
    tempBytes[2] = buffer[175];
    tempBytes[1] = buffer[176];
    tempBytes[0] = buffer[177];

    ByteBuffer bb = ByteBuffer.wrap(tempBytes);
    initialTS32 = bb.getInt();
    System.out.println("Initial Timestamp: " + initialTS32);

    for (int i = 0; i < buffer.length; i++) {
        //byteToBits(buffer[i], i);
    }
    return nRead;
}

From source file:org.dspace.app.util.Util.java

/**
  * Formats the file size. Examples:/*  w w  w  .  j  a  va  2s .  c o m*/
  *
  *  - 50 = 50B
  *  - 1024 = 1KB
  *  - 1,024,000 = 1MB etc
  *
  *  The numbers are formatted using java Locales
  *
  * @param in The number to convert
  * @return the file size as a String
  */
public static String formatFileSize(double in) {
    // Work out the size of the file, and format appropriatly
    // FIXME: When full i18n support is available, use the user's Locale
    // rather than the default Locale.
    NumberFormat nf = NumberFormat.getNumberInstance(Locale.getDefault());
    DecimalFormat df = (DecimalFormat) nf;
    df.applyPattern("###,###.##");
    if (in < 1024) {
        df.applyPattern("0");
        return df.format(in) + " " + "B";
    } else if (in < 1024000) {
        in = in / 1024;
        return df.format(in) + " " + "kB";
    } else if (in < 1024000000) {
        in = in / 1024000;
        return df.format(in) + " " + "MB";
    } else {
        in = in / 1024000000;
        return df.format(in) + " " + "GB";
    }
}

From source file:org.sonar.core.i18n.DefaultI18n.java

@Override
public String formatInteger(Locale locale, Integer value) {
    return NumberFormat.getNumberInstance(locale).format(value);
}

From source file:com.jeeframework.util.validate.GenericTypeValidator.java

/**
 *  Checks if the value can safely be converted to a long primitive.
 *
 *@param  value   The value validation is being performed on.
 *@param  locale  The locale to use to parse the number (system default if
 *      null)//w w w .java2 s  .c o m
 *@return the converted Long value.
 */
public static Long formatLong(String value, Locale locale) {
    Long result = null;

    if (value != null) {
        NumberFormat formatter = null;
        if (locale != null) {
            formatter = NumberFormat.getNumberInstance(locale);
        } else {
            formatter = NumberFormat.getNumberInstance(Locale.getDefault());
        }
        formatter.setParseIntegerOnly(true);
        ParsePosition pos = new ParsePosition(0);
        Number num = formatter.parse(value, pos);

        // If there was no error      and we used the whole string
        if (pos.getErrorIndex() == -1 && pos.getIndex() == value.length()) {
            if (num.doubleValue() >= Long.MIN_VALUE && num.doubleValue() <= Long.MAX_VALUE) {
                result = new Long(num.longValue());
            }
        }
    }

    return result;
}

From source file:easyJ.common.validate.GenericTypeValidator.java

/**
 * Checks if the value can safely be converted to a long primitive.
 * //from w ww  . java2s.  c o m
 * @param value
 *                The value validation is being performed on.
 * @param locale
 *                The locale to use to parse the number (system default if
 *                null)
 * @return the converted Long value.
 */
public static Long formatLong(String value, Locale locale) {
    Long result = null;

    if (value != null) {
        NumberFormat formatter = null;
        if (locale != null) {
            formatter = NumberFormat.getNumberInstance(locale);
        } else {
            formatter = NumberFormat.getNumberInstance(Locale.getDefault());
        }
        formatter.setParseIntegerOnly(true);
        ParsePosition pos = new ParsePosition(0);
        Number num = formatter.parse(value, pos);

        // If there was no error and we used the whole string
        if (pos.getErrorIndex() == -1 && pos.getIndex() == value.length()) {
            if (num.doubleValue() >= Long.MIN_VALUE && num.doubleValue() <= Long.MAX_VALUE) {
                result = new Long(num.longValue());
            }
        }
    }

    return result;
}