Java Tutorial/Data Type/Number Format
Содержание
- 1 Add leading zeros to a number
- 2 A number formatter for logarithmic values. This formatter does not support parsing.
- 3 Applied to strings, the precision specifier specifies the maximum field length
- 4 Format a percentage for presentation to the user
- 5 Get Percent Value
- 6 Helper class for format number and currency
- 7 Illustrating the precision specifier
- 8 NumberFormat.getCurrencyInstance(Locale.ENGLISH)
- 9 NumberFormat.getInstance()
- 10 NumberFormat.getPercentInstance(Locale.ENGLISH)
- 11 NumberFormat: Minimum Integer Digits, Maximum/Minimum Fraction Digits
- 12 Number formatting helps make your numbers more readable.
- 13 Number format with FieldPosition
- 14 Specifying Precision
Add leading zeros to a number
public class Main {
public static void main(String[] args) {
int number = 1500;
String formatted = String.format("%07d", number);
System.out.println("Number with leading zeros: " + formatted);
}
}
A number formatter for logarithmic values. This formatter does not support parsing.
/*
* JFreeChart : a free chart library for the Java(tm) platform
*
*
* (C) Copyright 2000-2009, by Object Refinery Limited and Contributors.
*
* Project Info: http://www.jfree.org/jfreechart/index.html
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA.
*
* [Java is a trademark or registered trademark of Sun Microsystems, Inc.
* in the United States and other countries.]
*
* --------------
* LogFormat.java
* --------------
* (C) Copyright 2007-2009, by Object Refinery Limited and Contributors.
*
* Original Author: David Gilbert (for Object Refinery Limited);
* Contributor(s): -;
*
* Changes
* -------
* 02-Aug-2007 : Version 1 (DG);
* 19-Feb-2008 : Implemented equals() and clone(), and added new powerLabel
* attribute as per Feature Request 1886036 (DG);
* 14-Jan-2009 : Added default constructor, and accessor methods for
* exponent formatter (DG);
*
*/
import java.text.DecimalFormat;
import java.text.FieldPosition;
import java.text.NumberFormat;
import java.text.ParsePosition;
/**
* A number formatter for logarithmic values. This formatter does not support
* parsing.
*
* @since 1.0.7
*/
public class LogFormat extends NumberFormat {
/** The log base value. */
private double base;
/** The natural logarithm of the base value. */
private double baseLog;
/** The label for the log base (for example, "e"). */
private String baseLabel;
/**
* The label for the power symbol.
*
* @since 1.0.10
*/
private String powerLabel;
/** A flag that controls whether or not the base is shown. */
private boolean showBase;
/** The number formatter for the exponent. */
private NumberFormat formatter = new DecimalFormat("0.0#");
/**
* Creates a new instance using base 10.
*
* @since 1.0.13
*/
public LogFormat() {
this(10.0, "10", true);
}
/**
* Creates a new instance.
*
* @param base the base.
* @param baseLabel the base label (<code>null</code> not permitted).
* @param showBase a flag that controls whether or not the base value is
* shown.
*/
public LogFormat(double base, String baseLabel, boolean showBase) {
this(base, baseLabel, "^", showBase);
}
/**
* Creates a new instance.
*
* @param base the base.
* @param baseLabel the base label (<code>null</code> not permitted).
* @param powerLabel the power label (<code>null</code> not permitted).
* @param showBase a flag that controls whether or not the base value is
* shown.
*
* @since 1.0.10
*/
public LogFormat(double base, String baseLabel, String powerLabel,
boolean showBase) {
if (baseLabel == null) {
throw new IllegalArgumentException("Null "baseLabel" argument.");
}
if (powerLabel == null) {
throw new IllegalArgumentException("Null "powerLabel" argument.");
}
this.base = base;
this.baseLog = Math.log(this.base);
this.baseLabel = baseLabel;
this.showBase = showBase;
this.powerLabel = powerLabel;
}
/**
* Returns the number format used for the exponent.
*
* @return The number format (never <code>null</code>).
*
* @since 1.0.13.
*/
public NumberFormat getExponentFormat() {
return (NumberFormat) this.formatter.clone();
}
/**
* Sets the number format used for the exponent.
*
* @param format the formatter (<code>null</code> not permitted).
*
* @since 1.0.13
*/
public void setExponentFormat(NumberFormat format) {
if (format == null) {
throw new IllegalArgumentException("Null "format" argument.");
}
this.formatter = format;
}
/**
* Calculates the log of a given value.
*
* @param value the value.
*
* @return The log of the value.
*/
private double calculateLog(double value) {
return Math.log(value) / this.baseLog;
}
/**
* Returns a formatted representation of the specified number.
*
* @param number the number.
* @param toAppendTo the string buffer to append to.
* @param pos the position.
*
* @return A string buffer containing the formatted value.
*/
public StringBuffer format(double number, StringBuffer toAppendTo,
FieldPosition pos) {
StringBuffer result = new StringBuffer();
if (this.showBase) {
result.append(this.baseLabel);
result.append(this.powerLabel);
}
result.append(this.formatter.format(calculateLog(number)));
return result;
}
/**
* Formats the specified number as a hexadecimal string. The decimal
* fraction is ignored.
*
* @param number the number to format.
* @param toAppendTo the buffer to append to (ignored here).
* @param pos the field position (ignored here).
*
* @return The string buffer.
*/
public StringBuffer format(long number, StringBuffer toAppendTo,
FieldPosition pos) {
StringBuffer result = new StringBuffer();
if (this.showBase) {
result.append(this.baseLabel);
result.append("^");
}
result.append(this.formatter.format(calculateLog(number)));
return result;
}
/**
* Parsing is not implemented, so this method always returns
* <code>null</code>.
*
* @param source ignored.
* @param parsePosition ignored.
*
* @return Always <code>null</code>.
*/
public Number parse (String source, ParsePosition parsePosition) {
return null; // don"t bother with parsing
}
/**
* Tests this formatter for equality with an arbitrary object.
*
* @param obj the object (<code>null</code> permitted).
*
* @return A boolean.
*/
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (!(obj instanceof LogFormat)) {
return false;
}
LogFormat that = (LogFormat) obj;
if (this.base != that.base) {
return false;
}
if (!this.baseLabel.equals(that.baseLabel)) {
return false;
}
if (this.baseLog != that.baseLog) {
return false;
}
if (this.showBase != that.showBase) {
return false;
}
if (!this.formatter.equals(that.formatter)) {
return false;
}
return super.equals(obj);
}
/**
* Returns a clone of this instance.
*
* @return A clone.
*/
public Object clone() {
LogFormat clone = (LogFormat) super.clone();
clone.formatter = (NumberFormat) this.formatter.clone();
return clone;
}
}
Applied to strings, the precision specifier specifies the maximum field length
For example, %5.7s displays a string at least five and not exceeding seven characters long. If the string is longer than the maximum field width, the end characters will be truncated.
import java.util.Formatter;
public class MainClass {
public static void main(String args[]) {
Formatter fmt;
fmt = new Formatter();
fmt.format("%5.7s", "abcdefghijklmn");
System.out.println(fmt);
fmt = new Formatter();
fmt.format("%5.7s", "abc");
System.out.println(fmt);
}
}
abcdefg abc
Format a percentage for presentation to the user
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.InetAddress;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.UnknownHostException;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.StringTokenizer;
import java.util.Collection;
/**
* General string utils
*/
public class StringUtils {
final public static char COMMA = ",";
final public static String COMMA_STR = ",";
final public static char ESCAPE_CHAR = "\\";
private static DecimalFormat oneDecimal = new DecimalFormat("0.0");
/**
* Format a percentage for presentation to the user.
* @param done the percentage to format (0.0 to 1.0)
* @param digits the number of digits past the decimal point
* @return a string representation of the percentage
*/
public static String formatPercent(double done, int digits) {
DecimalFormat percentFormat = new DecimalFormat("0.00%");
double scale = Math.pow(10.0, digits+2);
double rounded = Math.floor(done * scale);
percentFormat.setDecimalSeparatorAlwaysShown(false);
percentFormat.setMinimumFractionDigits(digits);
percentFormat.setMaximumFractionDigits(digits);
return percentFormat.format(rounded / scale);
}
}
Get Percent Value
/*
* Cobertura - http://cobertura.sourceforge.net/
*
* Copyright (C) 2005 Jeremy Thomerson
*
* Note: This file is dual licensed under the GPL and the Apache
* Source License (so that it can be used from both the main
* Cobertura classes and the ant tasks).
*
* Cobertura is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published
* by the Free Software Foundation; either version 2 of the License,
* or (at your option) any later version.
*
* Cobertura is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Cobertura; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA
*/
import java.text.NumberFormat;
/**
* Abstract, not to be instantiated utility class for String functions.
*
* @author Jeremy Thomerson
*/
public abstract class StringUtil
{
/**
*
* Replaces all instances of "replace" with "with" from the "original"
* string.
*
*
*
* NOTE: it is known that a similar function is included in jdk 1.4 as replaceAll(),
* but is written here so as to allow backward compatibility to users using SDK"s
* prior to 1.4
*
*
* @param original The original string to do replacement on.
* @param replace The string to replace.
* @param with The string to replace "replace" with.
* @return The replaced string.
*/
public static String replaceAll(String original, String replace, String with)
{
if (original == null)
{
return original;
}
final int len = replace.length();
StringBuffer sb = new StringBuffer(original.length());
int start = 0;
int found = -1;
while ((found = original.indexOf(replace, start)) != -1)
{
sb.append(original.substring(start, found));
sb.append(with);
start = found + len;
}
sb.append(original.substring(start));
return sb.toString();
}
/**
* Takes a double and turns it into a percent string.
* Ex. 0.5 turns into 50%
*
* @param value
* @return corresponding percent string
*/
public static String getPercentValue(double value)
{
//moved from HTMLReport.getPercentValue()
value = Math.floor(value * 100) / 100; //to represent 199 covered lines from 200 as 99% covered, not 100 %
return NumberFormat.getPercentInstance().format(value);
}
}
Helper class for format number and currency
/* Copyright 2004 Sun Microsystems, Inc. All rights reserved. You may not modify, use, reproduce, or distribute this software except in compliance with the terms of the License at:
http://adventurebuilder.dev.java.net/LICENSE.txt
$Id: I18nUtil.java,v 1.2 2004/05/26 00:07:34 inder Exp $ */
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Locale;
/**
* This utility class for internationalization. This class provides a central
* location to do specialized formatting in both a default and a locale specfic
* manner.
*/
public class Main {
public static String formatCurrency(double amount, int precision, String pattern, Locale locale) {
NumberFormat nf = NumberFormat.getCurrencyInstance(locale);
DecimalFormat df = (DecimalFormat) nf;
df.setMinimumFractionDigits(precision);
df.setMaximumFractionDigits(precision);
df.setDecimalSeparatorAlwaysShown(true);
df.applyPattern(pattern);
return df.format(amount);
}
public static String formatNumber(double amount, int precision, String pattern, Locale locale) {
NumberFormat nf = NumberFormat.getNumberInstance(locale);
DecimalFormat df = (DecimalFormat) nf;
df.setMinimumFractionDigits(precision);
df.setMaximumFractionDigits(precision);
df.setDecimalSeparatorAlwaysShown(true);
df.applyPattern(pattern);
return df.format(amount);
}
public static String formatCurrency(double amount, int precision, Locale locale) {
NumberFormat nf = NumberFormat.getCurrencyInstance(locale);
nf.setMinimumFractionDigits(precision);
nf.setMaximumFractionDigits(precision);
return nf.format(amount);
}
public static String formatNumber(double amount, int precision, Locale locale) {
NumberFormat nf = NumberFormat.getNumberInstance(locale);
nf.setMinimumFractionDigits(precision);
nf.setMaximumFractionDigits(precision);
return nf.format(amount);
}
}
Illustrating the precision specifier
123.1235
NumberFormat.getCurrencyInstance(Locale.ENGLISH)
import java.text.NumberFormat;
import java.util.Locale;
public class MainClass {
public static void main(String[] args) {
NumberFormat dollarFormat = NumberFormat.getCurrencyInstance(Locale.ENGLISH);
double minimumWage = 5.15;
System.out.println(dollarFormat.format(minimumWage));
System.out.println(dollarFormat.format(40 * 52 * minimumWage));
}
}
5.15 10,712.00
NumberFormat.getInstance()
import java.text.NumberFormat;
public class MainClass {
public static void main(String[] args) {
NumberFormat nf = NumberFormat.getInstance();
for (double x = Math.PI; x < 100000; x *= 10) {
String formattedNumber = nf.format(x);
System.out.println(formattedNumber + "\t" + x);
}
}
}
3.142 3.141592653589793 31.416 31.41592653589793 314.159 314.1592653589793 3,141.593 3141.5926535897934 31,415.927 31415.926535897932
NumberFormat.getPercentInstance(Locale.ENGLISH)
import java.text.NumberFormat;
import java.util.Locale;
public class MainClass {
public static void main(String[] args) {
NumberFormat percentFormat = NumberFormat.getPercentInstance(Locale.ENGLISH);
for (double d = 0.0; d <= 1.0; d += 0.005) {
System.out.println(percentFormat.format(d));
}
}
}
0% 0% 1% 2% 2% 2% 3% 4% 4% 4% 5% 5% 6% 6% 7% 8% 8% 8% 9% 10% 10% 11% 11% 12% 12% 13% 13% 14% 14% 15% 15% 16% 16% 17% 17% 18% 18% 19% 19% 20% 20% 21% 21% 22% 22% 23% 23% 24% 24% 25% 25% 26% 26% 27% 27% 28% 28% 29% 29% 30% 30% 31% 31% 32% 32% 33% 33% 34% 34% 35% 35% 36% 36% 37% 37% 38% 38% 39% 39% 40% 40% 41% 41% 42% 42% 43% 43% 44% 44% 45% 45% 46% 46% 47% 47% 48% 48% 49% 49% 50% 50% 51% 51% 52% 52% 53% 53% 54% 54% 55% 55% 56% 56% 57% 57% 58% 58% 59% 59% 60% 60% 61% 61% 62% 62% 63% 63% 64% 64% 65% 65% 66% 66% 67% 67% 68% 68% 69% 69% 70% 70% 71% 71% 72% 72% 73% 73% 74% 74% 75% 75% 76% 76% 77% 77% 78% 78% 79% 79% 80% 80% 81% 81% 82% 82% 83% 83% 84% 84% 85% 85% 86% 86% 87% 87% 88% 88% 89% 89% 90% 90% 91% 91% 92% 92% 93% 93% 94% 94% 95% 95% 96% 96% 97% 97% 98% 98% 99% 99% 100%
NumberFormat: Minimum Integer Digits, Maximum/Minimum Fraction Digits
import java.text.NumberFormat;
public class MainClass {
public static void main(String[] args) {
NumberFormat myFormat = NumberFormat.getInstance();
myFormat.setMinimumIntegerDigits(3);
myFormat.setMaximumFractionDigits(2);
myFormat.setMinimumFractionDigits(2);
for (double d = 0.0; d < 360.0; d++) {
String radianString = myFormat.format(Math.PI * d / 180.0);
String gradString = myFormat.format(400 * d / 360);
String degreeString = myFormat.format(d);
System.out.println(degreeString + " " + radianString + " " + gradString);
}
}
}
000.00 000.00 000.00 001.00 000.02 001.11 002.00 000.03 002.22 003.00 000.05 003.33 004.00 000.07 004.44 005.00 000.09 005.56 006.00 000.10 006.67 007.00 000.12 007.78 008.00 000.14 008.89 009.00 000.16 010.00 010.00 000.17 011.11 011.00 000.19 012.22 012.00 000.21 013.33 013.00 000.23 014.44 014.00 000.24 015.56 015.00 000.26 016.67 016.00 000.28 017.78 017.00 000.30 018.89 018.00 000.31 020.00 019.00 000.33 021.11 020.00 000.35 022.22 021.00 000.37 023.33 022.00 000.38 024.44 023.00 000.40 025.56 024.00 000.42 026.67 025.00 000.44 027.78 026.00 000.45 028.89 027.00 000.47 030.00 028.00 000.49 031.11 029.00 000.51 032.22 030.00 000.52 033.33 031.00 000.54 034.44 032.00 000.56 035.56 033.00 000.58 036.67 034.00 000.59 037.78 035.00 000.61 038.89 036.00 000.63 040.00 037.00 000.65 041.11 038.00 000.66 042.22 039.00 000.68 043.33 040.00 000.70 044.44 041.00 000.72 045.56 042.00 000.73 046.67 043.00 000.75 047.78 044.00 000.77 048.89 045.00 000.79 050.00 046.00 000.80 051.11 047.00 000.82 052.22 048.00 000.84 053.33 049.00 000.86 054.44 050.00 000.87 055.56 051.00 000.89 056.67 052.00 000.91 057.78 053.00 000.93 058.89 054.00 000.94 060.00 055.00 000.96 061.11 056.00 000.98 062.22 057.00 000.99 063.33 058.00 001.01 064.44 059.00 001.03 065.56 060.00 001.05 066.67 061.00 001.06 067.78 062.00 001.08 068.89 063.00 001.10 070.00 064.00 001.12 071.11 065.00 001.13 072.22 066.00 001.15 073.33 067.00 001.17 074.44 068.00 001.19 075.56 069.00 001.20 076.67 070.00 001.22 077.78 071.00 001.24 078.89 072.00 001.26 080.00 073.00 001.27 081.11 074.00 001.29 082.22 075.00 001.31 083.33 076.00 001.33 084.44 077.00 001.34 085.56 078.00 001.36 086.67 079.00 001.38 087.78 080.00 001.40 088.89 081.00 001.41 090.00 082.00 001.43 091.11 083.00 001.45 092.22 084.00 001.47 093.33 085.00 001.48 094.44 086.00 001.50 095.56 087.00 001.52 096.67 088.00 001.54 097.78 089.00 001.55 098.89 090.00 001.57 100.00 091.00 001.59 101.11 092.00 001.61 102.22 093.00 001.62 103.33 094.00 001.64 104.44 095.00 001.66 105.56 096.00 001.68 106.67 097.00 001.69 107.78 098.00 001.71 108.89 099.00 001.73 110.00 100.00 001.75 111.11 101.00 001.76 112.22 102.00 001.78 113.33 103.00 001.80 114.44 104.00 001.82 115.56 105.00 001.83 116.67 106.00 001.85 117.78 107.00 001.87 118.89 108.00 001.88 120.00 109.00 001.90 121.11 110.00 001.92 122.22 111.00 001.94 123.33 112.00 001.95 124.44 113.00 001.97 125.56 114.00 001.99 126.67 115.00 002.01 127.78 116.00 002.02 128.89 117.00 002.04 130.00 118.00 002.06 131.11 119.00 002.08 132.22 120.00 002.09 133.33 121.00 002.11 134.44 122.00 002.13 135.56 123.00 002.15 136.67 124.00 002.16 137.78 125.00 002.18 138.89 126.00 002.20 140.00 127.00 002.22 141.11 128.00 002.23 142.22 129.00 002.25 143.33 130.00 002.27 144.44 131.00 002.29 145.56 132.00 002.30 146.67 133.00 002.32 147.78 134.00 002.34 148.89 135.00 002.36 150.00 136.00 002.37 151.11 137.00 002.39 152.22 138.00 002.41 153.33 139.00 002.43 154.44 140.00 002.44 155.56 141.00 002.46 156.67 142.00 002.48 157.78 143.00 002.50 158.89 144.00 002.51 160.00 145.00 002.53 161.11 146.00 002.55 162.22 147.00 002.57 163.33 148.00 002.58 164.44 149.00 002.60 165.56 150.00 002.62 166.67 151.00 002.64 167.78 152.00 002.65 168.89 153.00 002.67 170.00 154.00 002.69 171.11 155.00 002.71 172.22 156.00 002.72 173.33 157.00 002.74 174.44 158.00 002.76 175.56 159.00 002.78 176.67 160.00 002.79 177.78 161.00 002.81 178.89 162.00 002.83 180.00 163.00 002.84 181.11 164.00 002.86 182.22 165.00 002.88 183.33 166.00 002.90 184.44 167.00 002.91 185.56 168.00 002.93 186.67 169.00 002.95 187.78 170.00 002.97 188.89 171.00 002.98 190.00 172.00 003.00 191.11 173.00 003.02 192.22 174.00 003.04 193.33 175.00 003.05 194.44 176.00 003.07 195.56 177.00 003.09 196.67 178.00 003.11 197.78 179.00 003.12 198.89 180.00 003.14 200.00 181.00 003.16 201.11 182.00 003.18 202.22 183.00 003.19 203.33 184.00 003.21 204.44 185.00 003.23 205.56 186.00 003.25 206.67 187.00 003.26 207.78 188.00 003.28 208.89 189.00 003.30 210.00 190.00 003.32 211.11 191.00 003.33 212.22 192.00 003.35 213.33 193.00 003.37 214.44 194.00 003.39 215.56 195.00 003.40 216.67 196.00 003.42 217.78 197.00 003.44 218.89 198.00 003.46 220.00 199.00 003.47 221.11 200.00 003.49 222.22 201.00 003.51 223.33 202.00 003.53 224.44 203.00 003.54 225.56 204.00 003.56 226.67 205.00 003.58 227.78 206.00 003.60 228.89 207.00 003.61 230.00 208.00 003.63 231.11 209.00 003.65 232.22 210.00 003.67 233.33 211.00 003.68 234.44 212.00 003.70 235.56 213.00 003.72 236.67 214.00 003.74 237.78 215.00 003.75 238.89 216.00 003.77 240.00 217.00 003.79 241.11 218.00 003.80 242.22 219.00 003.82 243.33 220.00 003.84 244.44 221.00 003.86 245.56 222.00 003.87 246.67 223.00 003.89 247.78 224.00 003.91 248.89 225.00 003.93 250.00 226.00 003.94 251.11 227.00 003.96 252.22 228.00 003.98 253.33 229.00 004.00 254.44 230.00 004.01 255.56 231.00 004.03 256.67 232.00 004.05 257.78 233.00 004.07 258.89 234.00 004.08 260.00 235.00 004.10 261.11 236.00 004.12 262.22 237.00 004.14 263.33 238.00 004.15 264.44 239.00 004.17 265.56 240.00 004.19 266.67 241.00 004.21 267.78 242.00 004.22 268.89 243.00 004.24 270.00 244.00 004.26 271.11 245.00 004.28 272.22 246.00 004.29 273.33 247.00 004.31 274.44 248.00 004.33 275.56 249.00 004.35 276.67 250.00 004.36 277.78 251.00 004.38 278.89 252.00 004.40 280.00 253.00 004.42 281.11 254.00 004.43 282.22 255.00 004.45 283.33 256.00 004.47 284.44 257.00 004.49 285.56 258.00 004.50 286.67 259.00 004.52 287.78 260.00 004.54 288.89 261.00 004.56 290.00 262.00 004.57 291.11 263.00 004.59 292.22 264.00 004.61 293.33 265.00 004.63 294.44 266.00 004.64 295.56 267.00 004.66 296.67 268.00 004.68 297.78 269.00 004.69 298.89 270.00 004.71 300.00 271.00 004.73 301.11 272.00 004.75 302.22 273.00 004.76 303.33 274.00 004.78 304.44 275.00 004.80 305.56 276.00 004.82 306.67 277.00 004.83 307.78 278.00 004.85 308.89 279.00 004.87 310.00 280.00 004.89 311.11 281.00 004.90 312.22 282.00 004.92 313.33 283.00 004.94 314.44 284.00 004.96 315.56 285.00 004.97 316.67 286.00 004.99 317.78 287.00 005.01 318.89 288.00 005.03 320.00 289.00 005.04 321.11 290.00 005.06 322.22 291.00 005.08 323.33 292.00 005.10 324.44 293.00 005.11 325.56 294.00 005.13 326.67 295.00 005.15 327.78 296.00 005.17 328.89 297.00 005.18 330.00 298.00 005.20 331.11 299.00 005.22 332.22 300.00 005.24 333.33 301.00 005.25 334.44 302.00 005.27 335.56 303.00 005.29 336.67 304.00 005.31 337.78 305.00 005.32 338.89 306.00 005.34 340.00 307.00 005.36 341.11 308.00 005.38 342.22 309.00 005.39 343.33 310.00 005.41 344.44 311.00 005.43 345.56 312.00 005.45 346.67 313.00 005.46 347.78 314.00 005.48 348.89 315.00 005.50 350.00 316.00 005.52 351.11 317.00 005.53 352.22 318.00 005.55 353.33 319.00 005.57 354.44 320.00 005.59 355.56 321.00 005.60 356.67 322.00 005.62 357.78 323.00 005.64 358.89 324.00 005.65 360.00 325.00 005.67 361.11 326.00 005.69 362.22 327.00 005.71 363.33 328.00 005.72 364.44 329.00 005.74 365.56 330.00 005.76 366.67 331.00 005.78 367.78 332.00 005.79 368.89 333.00 005.81 370.00 334.00 005.83 371.11 335.00 005.85 372.22 336.00 005.86 373.33 337.00 005.88 374.44 338.00 005.90 375.56 339.00 005.92 376.67 340.00 005.93 377.78 341.00 005.95 378.89 342.00 005.97 380.00 343.00 005.99 381.11 344.00 006.00 382.22 345.00 006.02 383.33 346.00 006.04 384.44 347.00 006.06 385.56 348.00 006.07 386.67 349.00 006.09 387.78 350.00 006.11 388.89 351.00 006.13 390.00 352.00 006.14 391.11 353.00 006.16 392.22 354.00 006.18 393.33 355.00 006.20 394.44 356.00 006.21 395.56 357.00 006.23 396.67 358.00 006.25 397.78 359.00 006.27 398.89
Number formatting helps make your numbers more readable.
import java.text.NumberFormat;
import java.util.Locale;
public class MainClass {
public static void main(String[] args) {
NumberFormat nf = NumberFormat.getInstance(Locale.US);
System.out.println(nf.getClass().getName());
System.out.println(nf.format(123445));
}
}
java.text.DecimalFormat 123,445
- If you use Locale.Germany, you"ll get a NumberFormat object that formats numbers according to the German locale.
- If you pass Locale.US, you get one for the US number format.
- The no-argument getInstance method returns a NumberFormat object with the user computer"s locale.
Number format with FieldPosition
import java.text.FieldPosition;
import java.text.NumberFormat;
public class MainClass {
public static void main(String[] args) {
NumberFormat myFormat = NumberFormat.getNumberInstance();
FieldPosition fp = new FieldPosition(NumberFormat.INTEGER_FIELD);
myFormat.setMaximumIntegerDigits(3);
myFormat.setMaximumFractionDigits(2);
myFormat.setMinimumFractionDigits(2);
System.out.println("Degrees Radians Grads");
for (double d = 0.0; d < 360.0; d++) {
String radianString = myFormat.format(Math.PI * d / 180.0, new StringBuffer(), fp)
.toString();
radianString = " " + radianString;
String gradString = myFormat.format(400 * d / 360, new StringBuffer(), fp).toString();
gradString = " " + gradString;
String degreeString = myFormat.format(d, new StringBuffer(), fp).toString();
degreeString = " " + degreeString;
System.out.println(degreeString + " " + radianString + " " + gradString);
}
}
}
Degrees Radians Grads 0.00 0.00 0.00 1.00 0.02 1.11 2.00 0.03 2.22 3.00 0.05 3.33 4.00 0.07 4.44 5.00 0.09 5.56 6.00 0.10 6.67 7.00 0.12 7.78 8.00 0.14 8.89 9.00 0.16 10.00 10.00 0.17 11.11 11.00 0.19 12.22 12.00 0.21 13.33 13.00 0.23 14.44 14.00 0.24 15.56 15.00 0.26 16.67 16.00 0.28 17.78 17.00 0.30 18.89 18.00 0.31 20.00 19.00 0.33 21.11 20.00 0.35 22.22 21.00 0.37 23.33 22.00 0.38 24.44 23.00 0.40 25.56 24.00 0.42 26.67 25.00 0.44 27.78 26.00 0.45 28.89 27.00 0.47 30.00 28.00 0.49 31.11 29.00 0.51 32.22 30.00 0.52 33.33 31.00 0.54 34.44 32.00 0.56 35.56 33.00 0.58 36.67 34.00 0.59 37.78 35.00 0.61 38.89 36.00 0.63 40.00 37.00 0.65 41.11 38.00 0.66 42.22 39.00 0.68 43.33 40.00 0.70 44.44 41.00 0.72 45.56 42.00 0.73 46.67 43.00 0.75 47.78 44.00 0.77 48.89 45.00 0.79 50.00 46.00 0.80 51.11 47.00 0.82 52.22 48.00 0.84 53.33 49.00 0.86 54.44 50.00 0.87 55.56 51.00 0.89 56.67 52.00 0.91 57.78 53.00 0.93 58.89 54.00 0.94 60.00 55.00 0.96 61.11 56.00 0.98 62.22 57.00 0.99 63.33 58.00 1.01 64.44 59.00 1.03 65.56 60.00 1.05 66.67 61.00 1.06 67.78 62.00 1.08 68.89 63.00 1.10 70.00 64.00 1.12 71.11 65.00 1.13 72.22 66.00 1.15 73.33 67.00 1.17 74.44 68.00 1.19 75.56 69.00 1.20 76.67 70.00 1.22 77.78 71.00 1.24 78.89 72.00 1.26 80.00 73.00 1.27 81.11 74.00 1.29 82.22 75.00 1.31 83.33 76.00 1.33 84.44 77.00 1.34 85.56 78.00 1.36 86.67 79.00 1.38 87.78 80.00 1.40 88.89 81.00 1.41 90.00 82.00 1.43 91.11 83.00 1.45 92.22 84.00 1.47 93.33 85.00 1.48 94.44 86.00 1.50 95.56 87.00 1.52 96.67 88.00 1.54 97.78 89.00 1.55 98.89 90.00 1.57 100.00 91.00 1.59 101.11 92.00 1.61 102.22 93.00 1.62 103.33 94.00 1.64 104.44 95.00 1.66 105.56 96.00 1.68 106.67 97.00 1.69 107.78 98.00 1.71 108.89 99.00 1.73 110.00 100.00 1.75 111.11 101.00 1.76 112.22 102.00 1.78 113.33 103.00 1.80 114.44 104.00 1.82 115.56 105.00 1.83 116.67 106.00 1.85 117.78 107.00 1.87 118.89 108.00 1.88 120.00 109.00 1.90 121.11 110.00 1.92 122.22 111.00 1.94 123.33 112.00 1.95 124.44 113.00 1.97 125.56 114.00 1.99 126.67 115.00 2.01 127.78 116.00 2.02 128.89 117.00 2.04 130.00 118.00 2.06 131.11 119.00 2.08 132.22 120.00 2.09 133.33 121.00 2.11 134.44 122.00 2.13 135.56 123.00 2.15 136.67 124.00 2.16 137.78 125.00 2.18 138.89 126.00 2.20 140.00 127.00 2.22 141.11 128.00 2.23 142.22 129.00 2.25 143.33 130.00 2.27 144.44 131.00 2.29 145.56 132.00 2.30 146.67 133.00 2.32 147.78 134.00 2.34 148.89 135.00 2.36 150.00 136.00 2.37 151.11 137.00 2.39 152.22 138.00 2.41 153.33 139.00 2.43 154.44 140.00 2.44 155.56 141.00 2.46 156.67 142.00 2.48 157.78 143.00 2.50 158.89 144.00 2.51 160.00 145.00 2.53 161.11 146.00 2.55 162.22 147.00 2.57 163.33 148.00 2.58 164.44 149.00 2.60 165.56 150.00 2.62 166.67 151.00 2.64 167.78 152.00 2.65 168.89 153.00 2.67 170.00 154.00 2.69 171.11 155.00 2.71 172.22 156.00 2.72 173.33 157.00 2.74 174.44 158.00 2.76 175.56 159.00 2.78 176.67 160.00 2.79 177.78 161.00 2.81 178.89 162.00 2.83 180.00 163.00 2.84 181.11 164.00 2.86 182.22 165.00 2.88 183.33 166.00 2.90 184.44 167.00 2.91 185.56 168.00 2.93 186.67 169.00 2.95 187.78 170.00 2.97 188.89 171.00 2.98 190.00 172.00 3.00 191.11 173.00 3.02 192.22 174.00 3.04 193.33 175.00 3.05 194.44 176.00 3.07 195.56 177.00 3.09 196.67 178.00 3.11 197.78 179.00 3.12 198.89 180.00 3.14 200.00 181.00 3.16 201.11 182.00 3.18 202.22 183.00 3.19 203.33 184.00 3.21 204.44 185.00 3.23 205.56 186.00 3.25 206.67 187.00 3.26 207.78 188.00 3.28 208.89 189.00 3.30 210.00 190.00 3.32 211.11 191.00 3.33 212.22 192.00 3.35 213.33 193.00 3.37 214.44 194.00 3.39 215.56 195.00 3.40 216.67 196.00 3.42 217.78 197.00 3.44 218.89 198.00 3.46 220.00 199.00 3.47 221.11 200.00 3.49 222.22 201.00 3.51 223.33 202.00 3.53 224.44 203.00 3.54 225.56 204.00 3.56 226.67 205.00 3.58 227.78 206.00 3.60 228.89 207.00 3.61 230.00 208.00 3.63 231.11 209.00 3.65 232.22 210.00 3.67 233.33 211.00 3.68 234.44 212.00 3.70 235.56 213.00 3.72 236.67 214.00 3.74 237.78 215.00 3.75 238.89 216.00 3.77 240.00 217.00 3.79 241.11 218.00 3.80 242.22 219.00 3.82 243.33 220.00 3.84 244.44 221.00 3.86 245.56 222.00 3.87 246.67 223.00 3.89 247.78 224.00 3.91 248.89 225.00 3.93 250.00 226.00 3.94 251.11 227.00 3.96 252.22 228.00 3.98 253.33 229.00 4.00 254.44 230.00 4.01 255.56 231.00 4.03 256.67 232.00 4.05 257.78 233.00 4.07 258.89 234.00 4.08 260.00 235.00 4.10 261.11 236.00 4.12 262.22 237.00 4.14 263.33 238.00 4.15 264.44 239.00 4.17 265.56 240.00 4.19 266.67 241.00 4.21 267.78 242.00 4.22 268.89 243.00 4.24 270.00 244.00 4.26 271.11 245.00 4.28 272.22 246.00 4.29 273.33 247.00 4.31 274.44 248.00 4.33 275.56 249.00 4.35 276.67 250.00 4.36 277.78 251.00 4.38 278.89 252.00 4.40 280.00 253.00 4.42 281.11 254.00 4.43 282.22 255.00 4.45 283.33 256.00 4.47 284.44 257.00 4.49 285.56 258.00 4.50 286.67 259.00 4.52 287.78 260.00 4.54 288.89 261.00 4.56 290.00 262.00 4.57 291.11 263.00 4.59 292.22 264.00 4.61 293.33 265.00 4.63 294.44 266.00 4.64 295.56 267.00 4.66 296.67 268.00 4.68 297.78 269.00 4.69 298.89 270.00 4.71 300.00 271.00 4.73 301.11 272.00 4.75 302.22 273.00 4.76 303.33 274.00 4.78 304.44 275.00 4.80 305.56 276.00 4.82 306.67 277.00 4.83 307.78 278.00 4.85 308.89 279.00 4.87 310.00 280.00 4.89 311.11 281.00 4.90 312.22 282.00 4.92 313.33 283.00 4.94 314.44 284.00 4.96 315.56 285.00 4.97 316.67 286.00 4.99 317.78 287.00 5.01 318.89 288.00 5.03 320.00 289.00 5.04 321.11 290.00 5.06 322.22 291.00 5.08 323.33 292.00 5.10 324.44 293.00 5.11 325.56 294.00 5.13 326.67 295.00 5.15 327.78 296.00 5.17 328.89 297.00 5.18 330.00 298.00 5.20 331.11 299.00 5.22 332.22 300.00 5.24 333.33 301.00 5.25 334.44 302.00 5.27 335.56 303.00 5.29 336.67 304.00 5.31 337.78 305.00 5.32 338.89 306.00 5.34 340.00 307.00 5.36 341.11 308.00 5.38 342.22 309.00 5.39 343.33 310.00 5.41 344.44 311.00 5.43 345.56 312.00 5.45 346.67 313.00 5.46 347.78 314.00 5.48 348.89 315.00 5.50 350.00 316.00 5.52 351.11 317.00 5.53 352.22 318.00 5.55 353.33 319.00 5.57 354.44 320.00 5.59 355.56 321.00 5.60 356.67 322.00 5.62 357.78 323.00 5.64 358.89 324.00 5.65 360.00 325.00 5.67 361.11 326.00 5.69 362.22 327.00 5.71 363.33 328.00 5.72 364.44 329.00 5.74 365.56 330.00 5.76 366.67 331.00 5.78 367.78 332.00 5.79 368.89 333.00 5.81 370.00 334.00 5.83 371.11 335.00 5.85 372.22 336.00 5.86 373.33 337.00 5.88 374.44 338.00 5.90 375.56 339.00 5.92 376.67 340.00 5.93 377.78 341.00 5.95 378.89 342.00 5.97 380.00 343.00 5.99 381.11 344.00 6.00 382.22 345.00 6.02 383.33 346.00 6.04 384.44 347.00 6.06 385.56 348.00 6.07 386.67 349.00 6.09 387.78 350.00 6.11 388.89 351.00 6.13 390.00 352.00 6.14 391.11 353.00 6.16 392.22 354.00 6.18 393.33 355.00 6.20 394.44 356.00 6.21 395.56 357.00 6.23 396.67 358.00 6.25 397.78 359.00 6.27 398.89
Specifying Precision
A precision specifier can be applied to the %f, %e, %g, and %s format specifiers.
When applying to floating-point data using the %f, %e, or %g specifiers, it determines the number of decimal places displayed.
The default precision is 6.
For example, %10.4f displays a number at least ten characters wide with four decimal places.
import java.util.Formatter;
public class MainClass {
public static void main(String args[]) {
Formatter fmt;
fmt = new Formatter();
fmt.format("%1.4f", 1234567890.123456789);
System.out.println(fmt);
}
}
1234567890.1235