# 將數字四捨五入至某小數位, 並返回指定位數字串

java_hs 2010-03-02 11:01:01
function2()
> 將數字四捨五入至某小數位, 並返回指定位數字串
> function2(123.455,2) => 123.46
> function2(123.449,2) => 123.45
> function2(123.44,3) => 123.440
> function2(123.4455,3) => 123.446
> function2(123.4499,3) => 123.450
> function2(123.9,0) => 124
> 程式接口:
> public static double function2(double value,int decimalPlaces);
>
...全文
184 15 打赏 收藏 转发到动态 举报

15 条回复

py330316117 2010-03-03
• 打赏
• 举报

Java code

{double a= Math.pow(10, i);return Math.floor(d* a+0.5)/ a;
}double two(double d,int i)
{double a= Math.pow(10, i);return Math.ceil(d* a-0.5)/ a;
}

=================================

+0.5然后采用floor这不是玩弄floor吗？

zm_hs 2010-03-03
• 打赏
• 举报

liguang168 2010-03-03
• 打赏
• 举报

Java code

{double a= Math.pow(10, i);return Math.floor(d* a+0.5)/ a;
}double two(double d,int i)
{double a= Math.pow(10, i);return Math.ceil(d* a-0.5)/ a;
}

=================================

+0.5然后采用floor这不是玩弄floor吗？
liguang168 2010-03-03
• 打赏
• 举报

Java code

{double a= Math.pow(10, i);return Math.floor(d* a+0.5)/ a;
}double two(double d,int i)
{double a= Math.pow(10, i);return Math.ceil(d* a-0.5)/ a;
}

=======================================================
System.out.println("one:" + one(123.4455,3));
System.out.println("two:" + two(123.4455,3));
System.out.println("function2:" + function2(123.4455,3));

System.out.println("\none:" + one(123.4499,3));
System.out.println("two:" + two(123.4499,3));
System.out.println("function2:" + function2(123.4499,3));

System.out.println("\none:" + one(123.455,2));
System.out.println("two:" + two(123.455,2));
System.out.println("function2:" + function2(123.455,2));

==========》
one:123.446
two:123.445
function2:123.446

one:123.45
two:123.45
function2:123.45

one:123.46
two:123.45
function2:123.46

py330316117 2010-03-02
• 打赏
• 举报

public static double function2(double value,int decimalPlaces){
double a=(int)(value*10^decimalPlace+0.5)/100.0;又问题这句话
return a;
}

dllzp1988 2010-03-02
• 打赏
• 举报

public static double function2(double value,int decimalPlaces){
double a=(int)(value*10^decimalPlace+0.5)/100.0;
return a;
}

Phoenix_99 2010-03-02
• 打赏
• 举报

``````
package com.nec.fastunit.common;

import java.math.BigDecimal;

public class FormatNumber {

public static String formatNumber(String number,int weishu){

// 判断字符串是否为空
if(number == null || (number.trim().length() == 0)){
//    System.out.println("输入的数字为空，请输入数字");
return "";
}

//  判断字符串里有除点号之外的非数字存在
char[] strArray = number.toCharArray();

for(int i = 0; i < strArray.length; i++){
if((strArray[i] < '0' || strArray[i] > '9')&& (strArray[i] != '.' && strArray[i] != '-' && strArray[i] != 'E')){
//    System.out.println("您输入的字符串中含有非法字符,请重新输入");
return number;
}else if(strArray[i] == 'E'){
if(strArray[0] == '-'){
number = number.substring(1,number.length());
String numstr = orinum(number);
numstr = "-" + numstr;
number = numstr;
}else{
number = orinum(number);
}

}
}

// 四舍五入后得数
number = String.valueOf(numz(Double.valueOf(number),weishu));

// 判断小数位数为0的情况
if(number.indexOf(".") != -1 && weishu == 0){

int pointNumber = number.indexOf(".");
String strFirst = number.substring(0,pointNumber);

// 小数点前字符串分割
strFirst = splitFirstNumber(strFirst);
return strFirst;

}

// 判断没有小数点，但小数位数要求有位数的情况
if(number.indexOf(".") == -1 && weishu != 0){

number = number + ".";
for(int i = 0; i < weishu; i++){
number = number + "0";
}

int pointNumber = number.indexOf(".");
String strFirst = number.substring(0,pointNumber);
String strLast = number.substring(pointNumber + 1,number.length());

// 小数点前字符串分割
strFirst = splitFirstNumber(strFirst);

// 小数点后字符串分割
strLast = splitLastNumber(strLast);

return strFirst + "." + strLast;

}

// 判断没有小数部分的情况
if((number.indexOf(".") == -1) && weishu == 0){

// 小数点前字符串分割
number = splitFirstNumber(number);
return number;
} else {
int pointNumber = number.indexOf(".");
String strFirst = number.substring(0,pointNumber);
String strLast = number.substring(pointNumber + 1,number.length());

// 小数位数判断
strLast = formatPoint(strLast,weishu);

// 小数点前字符串分割
strFirst = splitFirstNumber(strFirst);

// 小数点后字符串分割
strLast = splitLastNumber(strLast);

return strFirst + "." + strLast;

}
}

/**
*
* @param str 小数点前的字符串
* @return 返回小数点前的逗号分割
*/
public static  String splitFirstNumber(String str){
char[] strArray = str.toCharArray();
if(strArray[0] == '-'){
StringBuffer strBuffer = new StringBuffer();
strBuffer.append(str);
strBuffer.reverse();
str = strBuffer.toString();
StringBuffer sb = new StringBuffer();
for(int i = 0; i <  str.length(); i=i+3){
if(i+3 < str.length()){
String str1 = str.substring(i,i+3);
sb.append(str1);
if(i+3 == str.length() -1){
//
}else{
sb.append(",");
}
} else {
String str2 = str.substring(i,str.length());
sb.append(str2);
}
}
sb.reverse();
return sb.toString();

}else{
StringBuffer strBuffer = new StringBuffer();
strBuffer.append(str);
strBuffer.reverse();
str = strBuffer.toString();
StringBuffer sb = new StringBuffer();
for(int i = 0; i <  str.length(); i=i+3){
if(i+3 < str.length()){
String str1 = str.substring(i,i+3);
sb.append(str1);
sb.append(",");
} else {
String str2 = str.substring(i,str.length());
sb.append(str2);
}
}
sb.reverse();
return sb.toString();
}
}

/**
*
* @param str 小数点后字符串
* @return 逗号分割后的字符串
*/
public static String splitLastNumber(String str){
StringBuffer sb = new StringBuffer();
for(int i = 0; i <  str.length(); i=i+3){
if(i+3 < str.length()){
String str1 = str.substring(i,i+3);
sb.append(str1);
sb.append(",");
} else {
String str2 = str.substring(i,str.length());
sb.append(str2);
}
}
return sb.toString();
}

/**
*
* @param laststr 小数点后字符串
* @param bitWei 截取的字符串长度
* @return  返回小数点后应得的字符串
*/
public static String formatPoint(String laststr,int bitWei){
int n = laststr.length();
if(n < bitWei){
for(int i = 0; i < bitWei - n; i++){
laststr = laststr + "0";
}
} else {
BigDecimal bd = new BigDecimal("0." + laststr).setScale(bitWei,BigDecimal.ROUND_HALF_UP);
laststr = bd.toString();
laststr = laststr.substring(2,laststr.length());
}
return laststr;
}

// 处理科学计数法
public static String orinum(String str){
int index = str.indexOf("E");
String lstr = str.substring(index + 1, str.length());
int pindex = str.indexOf(".");
//  System.out.println("pindex: " + pindex);

// 取小数点后到E之间的字符串
String  midstr = str.substring(pindex + 1,index);

// 取小数点前字符
String pstr = str.substring(0,pindex);

// 判断E后数字是正还是负
Integer e = Integer.parseInt(lstr);
String nstr = null;
if(e > 0){
if(midstr.length() > e){
nstr = pstr + midstr.substring(0,e) + "." + midstr.subSequence(e, midstr.length());
}else{
nstr = pstr + midstr;
for(int i = 0; i < e  - midstr.length(); i++){
nstr = nstr  + "0";
}
}
}else{
if(pstr.length() > -e){
nstr = pstr.substring(0,pindex + e) + "." + pstr.substring(pindex+e,pstr.length()) + midstr;
}else{
nstr = pstr + midstr;
for(int i = 0; i < -e - pstr.length(); i++){
nstr = "0" + nstr;
}
nstr = "0." + nstr;
}
}
return nstr;
}

public static double numz(double numstr,int bitshu){
BigDecimal b = new BigDecimal(Double.toString(numstr));
String finalnum =  b.setScale(bitshu, BigDecimal.ROUND_HALF_EVEN).toString();
return Double.valueOf(finalnum);
}
}
``````

tianfengt04 2010-03-02
• 打赏
• 举报

dllzp1988 2010-03-02
• 打赏
• 举报

Java code

{double a= Math.pow(10, i);return Math.floor(d* a+0.5)/ a;
}double two(double d,int i)
{double a= Math.pow(10, i);return Math.ceil(d* a-0.5)/ a;
}

dllzp1988 2010-03-02
• 打赏
• 举报

Java codepublicstaticvoid main(String args[]){double value=123.9999;
String replacestr=new Double(value).toString();
FormatNumber formatnumber=new FormatNumber();
String str=formatnumber.formatNumber(replacestr,3);
System.out.println(str);
}

samihyypia 2010-03-02
• 打赏
• 举报

public static double function2(double value,int decimalPlaces){
double deciValue = Math.pow(10, decimalPlaces);
return Math.round(value * deciValue) / deciValue;
}
TillPerfect 2010-03-02
• 打赏
• 举报

``````

double one(double d, int i)
{
double a = Math.pow(10, i);
return Math.floor(d * a + 0.5) / a;
}

double two(double d, int i)
{
double a = Math.pow(10, i);
return Math.ceil(d * a - 0.5) / a;
}
``````

py330316117 2010-03-02
• 打赏
• 举报

``````public static void main(String args[]){
double value=123.9999;
String replacestr=new Double(value).toString();
FormatNumber formatnumber=new FormatNumber();
String str=formatnumber.formatNumber(replacestr, 3);
System.out.println(str);
}``````
zliuzz 2010-03-02
• 打赏
• 举报

liguang168 2010-03-02
• 打赏
• 举报

=================================================
public static double function2(double value,int decimalPlaces){
double deciValue = Math.pow(10, decimalPlaces);
return Math.ceil(value * deciValue) / deciValue;
}

62,623

• 近7日
• 近30日
• 至今