- 浏览: 258624 次
- 性别:
- 来自: 福州
文章分类
最新评论
-
zwhc:
finalerboy 写道有问题的。。。而且问题多得很,你自己 ...
将数字转为指定长度的字符串,如果位数不够,添加前缀 0 -
finalerboy:
有问题的。。。而且问题多得很,你自己试试
for(int j ...
将数字转为指定长度的字符串,如果位数不够,添加前缀 0 -
a542435582:
没有考虑中文哦,实现了三分之一
以 UE 十六进制编辑模式的显示方式显示数据 -
white_crucifix:
kyfxbl 写道左耳朵耗子这人感觉挺装的,发的帖子也没什么营 ...
atoi -
kyfxbl:
左耳朵耗子这人感觉挺装的,发的帖子也没什么营养,我已经取消关注 ...
atoi
DecimalFormat 源码简单阅读
最终应该是使用 subformat 来进行格式化的。
因为我主要是想看一下,"0000" 这种格式,源码里是如何进行格式化的,所以,其它细节忽略。
关注到这几行代码:
char zero = symbols.getZeroDigit();
int zeroDelta = zero - '0'; // '0' is the DigitList representation of zero
if (digitList.isZero()) {
digitList.decimalAt = 0; // Normalize
}
前缀 0 应该是这里添加的。
if (!fractionPresent && result.length() == sizeBeforeIntegerPart) {
result.append(zero);
}
查看了一下 symbols.getZeroDigit()
写得真是复杂。。。
最终应该是使用 subformat 来进行格式化的。
因为我主要是想看一下,"0000" 这种格式,源码里是如何进行格式化的,所以,其它细节忽略。
关注到这几行代码:
char zero = symbols.getZeroDigit();
int zeroDelta = zero - '0'; // '0' is the DigitList representation of zero
if (digitList.isZero()) {
digitList.decimalAt = 0; // Normalize
}
前缀 0 应该是这里添加的。
if (!fractionPresent && result.length() == sizeBeforeIntegerPart) {
result.append(zero);
}
查看了一下 symbols.getZeroDigit()
写得真是复杂。。。
private StringBuffer subformat(StringBuffer result, FieldDelegate delegate, boolean isNegative, boolean isInteger, int maxIntDigits, int minIntDigits, int maxFraDigits, int minFraDigits) { // NOTE: This isn't required anymore because DigitList takes care of this. // // // The negative of the exponent represents the number of leading // // zeros between the decimal and the first non-zero digit, for // // a value < 0.1 (e.g., for 0.00123, -fExponent == 2). If this // // is more than the maximum fraction digits, then we have an underflow // // for the printed representation. We recognize this here and set // // the DigitList representation to zero in this situation. // // if (-digitList.decimalAt >= getMaximumFractionDigits()) // { // digitList.count = 0; // } char zero = symbols.getZeroDigit(); int zeroDelta = zero - '0'; // '0' is the DigitList representation of zero char grouping = symbols.getGroupingSeparator(); char decimal = isCurrencyFormat ? symbols.getMonetaryDecimalSeparator() : symbols.getDecimalSeparator(); /* Per bug 4147706, DecimalFormat must respect the sign of numbers which * format as zero. This allows sensible computations and preserves * relations such as signum(1/x) = signum(x), where x is +Infinity or * -Infinity. Prior to this fix, we always formatted zero values as if * they were positive. Liu 7/6/98. */ if (digitList.isZero()) { digitList.decimalAt = 0; // Normalize } if (isNegative) { append(result, negativePrefix, delegate, getNegativePrefixFieldPositions(), Field.SIGN); } else { append(result, positivePrefix, delegate, getPositivePrefixFieldPositions(), Field.SIGN); } if (useExponentialNotation) { int iFieldStart = result.length(); int iFieldEnd = -1; int fFieldStart = -1; // Minimum integer digits are handled in exponential format by // adjusting the exponent. For example, 0.01234 with 3 minimum // integer digits is "123.4E-4". // Maximum integer digits are interpreted as indicating the // repeating range. This is useful for engineering notation, in // which the exponent is restricted to a multiple of 3. For // example, 0.01234 with 3 maximum integer digits is "12.34e-3". // If maximum integer digits are > 1 and are larger than // minimum integer digits, then minimum integer digits are // ignored. int exponent = digitList.decimalAt; int repeat = maxIntDigits; int minimumIntegerDigits = minIntDigits; if (repeat > 1 && repeat > minIntDigits) { // A repeating range is defined; adjust to it as follows. // If repeat == 3, we have 6,5,4=>3; 3,2,1=>0; 0,-1,-2=>-3; // -3,-4,-5=>-6, etc. This takes into account that the // exponent we have here is off by one from what we expect; // it is for the format 0.MMMMMx10^n. if (exponent >= 1) { exponent = ((exponent - 1) / repeat) * repeat; } else { // integer division rounds towards 0 exponent = ((exponent - repeat) / repeat) * repeat; } minimumIntegerDigits = 1; } else { // No repeating range is defined; use minimum integer digits. exponent -= minimumIntegerDigits; } // We now output a minimum number of digits, and more if there // are more digits, up to the maximum number of digits. We // place the decimal point after the "integer" digits, which // are the first (decimalAt - exponent) digits. int minimumDigits = minIntDigits + minFraDigits; if (minimumDigits < 0) { // overflow? minimumDigits = Integer.MAX_VALUE; } // The number of integer digits is handled specially if the number // is zero, since then there may be no digits. int integerDigits = digitList.isZero() ? minimumIntegerDigits : digitList.decimalAt - exponent; if (minimumDigits < integerDigits) { minimumDigits = integerDigits; } int totalDigits = digitList.count; if (minimumDigits > totalDigits) { totalDigits = minimumDigits; } boolean addedDecimalSeparator = false; for (int i=0; i<totalDigits; ++i) { if (i == integerDigits) { // Record field information for caller. iFieldEnd = result.length(); result.append(decimal); addedDecimalSeparator = true; // Record field information for caller. fFieldStart = result.length(); } result.append((i < digitList.count) ? (char)(digitList.digits[i] + zeroDelta) : zero); } if (decimalSeparatorAlwaysShown && totalDigits == integerDigits) { // Record field information for caller. iFieldEnd = result.length(); result.append(decimal); addedDecimalSeparator = true; // Record field information for caller. fFieldStart = result.length(); } // Record field information if (iFieldEnd == -1) { iFieldEnd = result.length(); } delegate.formatted(INTEGER_FIELD, Field.INTEGER, Field.INTEGER, iFieldStart, iFieldEnd, result); if (addedDecimalSeparator) { delegate.formatted(Field.DECIMAL_SEPARATOR, Field.DECIMAL_SEPARATOR, iFieldEnd, fFieldStart, result); } if (fFieldStart == -1) { fFieldStart = result.length(); } delegate.formatted(FRACTION_FIELD, Field.FRACTION, Field.FRACTION, fFieldStart, result.length(), result); // The exponent is output using the pattern-specified minimum // exponent digits. There is no maximum limit to the exponent // digits, since truncating the exponent would result in an // unacceptable inaccuracy. int fieldStart = result.length(); result.append(symbols.getExponentSeparator()); delegate.formatted(Field.EXPONENT_SYMBOL, Field.EXPONENT_SYMBOL, fieldStart, result.length(), result); // For zero values, we force the exponent to zero. We // must do this here, and not earlier, because the value // is used to determine integer digit count above. if (digitList.isZero()) { exponent = 0; } boolean negativeExponent = exponent < 0; if (negativeExponent) { exponent = -exponent; fieldStart = result.length(); result.append(symbols.getMinusSign()); delegate.formatted(Field.EXPONENT_SIGN, Field.EXPONENT_SIGN, fieldStart, result.length(), result); } digitList.set(negativeExponent, exponent); int eFieldStart = result.length(); for (int i=digitList.decimalAt; i<minExponentDigits; ++i) { result.append(zero); } for (int i=0; i<digitList.decimalAt; ++i) { result.append((i < digitList.count) ? (char)(digitList.digits[i] + zeroDelta) : zero); } delegate.formatted(Field.EXPONENT, Field.EXPONENT, eFieldStart, result.length(), result); } else { int iFieldStart = result.length(); // Output the integer portion. Here 'count' is the total // number of integer digits we will display, including both // leading zeros required to satisfy getMinimumIntegerDigits, // and actual digits present in the number. int count = minIntDigits; int digitIndex = 0; // Index into digitList.fDigits[] if (digitList.decimalAt > 0 && count < digitList.decimalAt) { count = digitList.decimalAt; } // Handle the case where getMaximumIntegerDigits() is smaller // than the real number of integer digits. If this is so, we // output the least significant max integer digits. For example, // the value 1997 printed with 2 max integer digits is just "97". if (count > maxIntDigits) { count = maxIntDigits; digitIndex = digitList.decimalAt - count; } int sizeBeforeIntegerPart = result.length(); for (int i=count-1; i>=0; --i) { if (i < digitList.decimalAt && digitIndex < digitList.count) { // Output a real digit result.append((char)(digitList.digits[digitIndex++] + zeroDelta)); } else { // Output a leading zero result.append(zero); } // Output grouping separator if necessary. Don't output a // grouping separator if i==0 though; that's at the end of // the integer part. if (isGroupingUsed() && i>0 && (groupingSize != 0) && (i % groupingSize == 0)) { int gStart = result.length(); result.append(grouping); delegate.formatted(Field.GROUPING_SEPARATOR, Field.GROUPING_SEPARATOR, gStart, result.length(), result); } } // Determine whether or not there are any printable fractional // digits. If we've used up the digits we know there aren't. boolean fractionPresent = (minFraDigits > 0) || (!isInteger && digitIndex < digitList.count); // If there is no fraction present, and we haven't printed any // integer digits, then print a zero. Otherwise we won't print // _any_ digits, and we won't be able to parse this string. if (!fractionPresent && result.length() == sizeBeforeIntegerPart) { result.append(zero); } delegate.formatted(INTEGER_FIELD, Field.INTEGER, Field.INTEGER, iFieldStart, result.length(), result); // Output the decimal separator if we always do so. int sStart = result.length(); if (decimalSeparatorAlwaysShown || fractionPresent) { result.append(decimal); } if (sStart != result.length()) { delegate.formatted(Field.DECIMAL_SEPARATOR, Field.DECIMAL_SEPARATOR, sStart, result.length(), result); } int fFieldStart = result.length(); for (int i=0; i < maxFraDigits; ++i) { // Here is where we escape from the loop. We escape if we've // output the maximum fraction digits (specified in the for // expression above). // We also stop when we've output the minimum digits and either: // we have an integer, so there is no fractional stuff to // display, or we're out of significant digits. if (i >= minFraDigits && (isInteger || digitIndex >= digitList.count)) { break; } // Output leading fractional zeros. These are zeros that come // after the decimal but before any significant digits. These // are only output if abs(number being formatted) < 1.0. if (-1-i > (digitList.decimalAt-1)) { result.append(zero); continue; } // Output a digit, if we have any precision left, or a // zero if we don't. We don't want to output noise digits. if (!isInteger && digitIndex < digitList.count) { result.append((char)(digitList.digits[digitIndex++] + zeroDelta)); } else { result.append(zero); } } // Record field information for caller. delegate.formatted(FRACTION_FIELD, Field.FRACTION, Field.FRACTION, fFieldStart, result.length(), result); } if (isNegative) { append(result, negativeSuffix, delegate, getNegativeSuffixFieldPositions(), Field.SIGN); } else { append(result, positiveSuffix, delegate, getPositiveSuffixFieldPositions(), Field.SIGN); } return result; }
发表评论
-
递归查找指定的目录,将所有的md转换成html
2015-02-16 13:33 1650在 github.com fork 了 markdown2ht ... -
弈城围棋棋谱下载,附源码
2014-06-10 21:55 2452弈城围棋的解说用的好象是 sgf 第一版的。 平时我们用的 s ... -
appfuse 非常棒
2014-05-31 14:17 1220appfuse 非常棒 【1】在线 demo http:// ... -
stripes spring annotation
2014-05-02 14:19 775stripes 源码阅读 stripes-code-1527 ... -
junit annotation
2014-05-02 01:15 1197写了个简单的 junit 启动器。 package ... -
一种变形全排列算法
2013-06-07 17:39 1411public static boolean next(i ... -
typoglycemia
2012-11-05 23:52 1519单词里面字母乱序不影响阅读的现象,学名叫做Typoglycem ... -
axis 源码初步研读
2012-10-16 15:41 1605经过反复跟踪,这段代码比较重要: org.apache.ax ... -
电信彩信附件,webservice HttpURLConnection
2012-10-16 15:26 1730电信彩信附件,webservice HttpURLConnec ... -
java final parameter
2012-09-14 19:28 1078一些代码里,参数前加了 final 修饰符,不太明白这样做的原 ... -
hibernate like in 多表 查询
2012-09-07 12:24 1553public List getMaintainList( ... -
学一篇韵文,识天下汉字
2012-08-20 15:49 1671学一篇韵文,识天下汉字 http://blog.renren. ... -
简易建表工具
2012-07-19 16:56 0简易建表工具 生成 oracle 表。 -
正则表达式测试工具。
2012-05-29 17:47 1152http://docs.oracle.com/javase/t ... -
关于 《Java 性能优化之 String 篇》 的疑问
2012-05-18 11:55 1132原文地址在: http://www.ibm.com/devel ... -
java char
2012-04-24 09:30 3043刚才简单研究了一下 char 。 比想象的复杂得多。主要应该 ... -
java 与 iso-8859-1 编码
2012-04-20 10:58 11575今天又研究了一下字符编码。写了些测试代码,算是比较了解了。 ... -
java 死循环
2012-03-08 15:46 1013写了这么段代码,居然出现死循环了。真晕。 /** ... -
利息计算
2011-11-29 01:00 1239/** * 利息计算: * 借款 12000,一年 ... -
Enum 如何继承?
2011-11-08 20:30 2927Enum 如何继承? public abstract clas ...
相关推荐
DecimalFormat精度解决,商业运算精度问题,BigDecimal类的应用--------------
主要介绍了java 使用DecimalFormat进行数字的格式化实例详解的相关资料,需要的朋友可以参考下
主要为大家详细介绍了DecimalFormat的多种用法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
主要为大家详细介绍了Java中DecimalFormat的用法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
主要为大家详细介绍了java DecimalFormat的常用方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
decimalformat是Java的类的Node实现。 安装 npm install @deloitte-digital-au/decimalformat 用法 使用DecimalFormat类 const { DecimalFormat } = require ( 'DecimalFormat' ) ; const df = new DecimalFormat ...
这个源码资源是一个讲解数学计算处理类的示例项目,旨在帮助开发者理解和使用Java中的数学计算相关类库。该项目包含详细的代码示例和注释,涵盖了以下几个主要的数学计算处理类:BigDecimal、Math、Random和...
主要为大家详细介绍了DecimalFormat数字格式化用法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
主要为大家详细介绍了java.text.DecimalFormat用法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
主要为大家详细介绍了java.text.DecimalFormat类十进制格式化的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
主要为大家详细介绍了java中DecimalFormat四舍五入的用法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
格式化字符串(数字,时间)。 DecimalFormat format=new DecimalFormat("#,####.##"); //创建格式化字符串的对象
DecimalFormat可以实现把数字划分为一定的格式。 具体程序如下: import java.io.File; import java.text.DecimalFormat; import android.app.Activity; import android.os.Bundle; import android.os.Environment;
1、格式化小数 123456.123 -> 123,456.12 2、格式化整数 123456 -> 123,456
DecimalFormat format = new DecimalFormat(#.##); String numberStr = format.format(numberDecimal); Log.e(TAG,numberStr => + numberStr); 2.第二种方法:使用String.split()函数 String price = priceET....
第一种方法-使用DecimalFormat类 举个例子,假如我们需要保留两位小数,我们可以这样写 DecimalFormat df = new DecimalFormat(0.00); 测试如下: double d = 0.200; DecimalFormat df = new ...
import java.text.DecimalFormat; import java.util.Vector; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android....
源码如下: import java.awt.*; import java.text.DecimalFormat; import java.awt.event.*; import javax.swing.*;
这种方式处理保留2位小数很方便,刚开始的时候我自己写if判断太麻烦了,最后知道用DecimalFormat 格式化,就可以把整数小数 都保留2位小数,分享给大家。