Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/156.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
四舍五入BigDecimal以匹配C++;把格式数据写成串 我正在把一个C++应用程序移植到java。如何匹配sprintf的舍入行为 int main() { char result[20]; double dArr [6] = {5.05, 5.15, 5.25, 5.35, 5.45, 5.55}; for(int i; i< 6; i++) { sprintf(result,"%3.1lf",dArr[i]); cout << result << endl; } return 0; } VALUE C++ sprintf Rounding: --------- ---------------------------------------- 5.05 - 5.0 (rounding: half_down or half_even?) 5.15 - 5.2 (rounding: half_up or half_even?) 5.25 - 5.2 (rounding: half_down or half_even?) 5.35 - 5.3 (rounding: half_down or half_odd ?) 5.45 - 5.5 (rounding: half_up or half_odd ?) 5.55 - 5.5 (rounding: half_down or half_odd ?)_Java_C++_Rounding_Bigdecimal - Fatal编程技术网

四舍五入BigDecimal以匹配C++;把格式数据写成串 我正在把一个C++应用程序移植到java。如何匹配sprintf的舍入行为 int main() { char result[20]; double dArr [6] = {5.05, 5.15, 5.25, 5.35, 5.45, 5.55}; for(int i; i< 6; i++) { sprintf(result,"%3.1lf",dArr[i]); cout << result << endl; } return 0; } VALUE C++ sprintf Rounding: --------- ---------------------------------------- 5.05 - 5.0 (rounding: half_down or half_even?) 5.15 - 5.2 (rounding: half_up or half_even?) 5.25 - 5.2 (rounding: half_down or half_even?) 5.35 - 5.3 (rounding: half_down or half_odd ?) 5.45 - 5.5 (rounding: half_up or half_odd ?) 5.55 - 5.5 (rounding: half_down or half_odd ?)

四舍五入BigDecimal以匹配C++;把格式数据写成串 我正在把一个C++应用程序移植到java。如何匹配sprintf的舍入行为 int main() { char result[20]; double dArr [6] = {5.05, 5.15, 5.25, 5.35, 5.45, 5.55}; for(int i; i< 6; i++) { sprintf(result,"%3.1lf",dArr[i]); cout << result << endl; } return 0; } VALUE C++ sprintf Rounding: --------- ---------------------------------------- 5.05 - 5.0 (rounding: half_down or half_even?) 5.15 - 5.2 (rounding: half_up or half_even?) 5.25 - 5.2 (rounding: half_down or half_even?) 5.35 - 5.3 (rounding: half_down or half_odd ?) 5.45 - 5.5 (rounding: half_up or half_odd ?) 5.55 - 5.5 (rounding: half_down or half_odd ?),java,c++,rounding,bigdecimal,Java,C++,Rounding,Bigdecimal,当我尝试将数字四舍五入到BigDecimal.round_HALF_偶数: System.out.println(getClassaverage() + " - " + getClassaverage().setScale(1,BigDecimal.ROUND_HALF_EVEN) ); 我将值“5.55”四舍五入为“5.6”。 在我修改了你的测试后,我明白了为什么会这样: @Test public void main() { double [] dArr = {5.05, 5.15,

当我尝试将数字四舍五入到BigDecimal.round_HALF_偶数:

System.out.println(getClassaverage() + " - " + getClassaverage().setScale(1,BigDecimal.ROUND_HALF_EVEN) );
我将值
“5.55”四舍五入为“5.6”。

在我修改了你的测试后,我明白了为什么会这样:

@Test
public void main() {
   double [] dArr = {5.05, 5.15, 5.25, 5.35, 5.45, 5.55};
   for (double d : dArr) {
      System.out.println(new BigDecimal(d) + " - " + new BigDecimal(d).setScale(1, BigDecimal.ROUND_HALF_EVEN));
   }
} 
这就是我得到的:

5.04999999999999982236431605997495353221893310546875 - 5.0
5.1500000000000003552713678800500929355621337890625 - 5.2
5.25 - 5.2
5.3499999999999996447286321199499070644378662109375 - 5.3
5.45000000000000017763568394002504646778106689453125 - 5.5
5.54999999999999982236431605997495353221893310546875 - 5.5
当我将
double[]
替换为
String[]
时,您的测试将生成
BigDecimal的预期值。四舍五入半偶数

5.05 - 5.0
5.15 - 5.2
5.25 - 5.2
5.35 - 5.4
5.45 - 5.4
5.55 - 5.6

我已经删除了我的解决方案,因为它是错误的。但是我会解释C++的四舍五入的不太明显的行为。

使用此C++代码揭示双倍的内部价值。

int main() {
    char result[20];
    double dArr [6] = {5.05, 5.15, 5.25, 5.35, 5.45, 5.55};
    for(int i = 0; i< 6; i++) {
        sprintf(result, "%3.16f  %3.1lf", dArr[i], dArr[i]);
        cout << result << endl;
    }
    return 0;
}
我最初的建议得到修正

double[] dArr = {5.05, 5.15, 5.25, 5.35, 5.45, 5.55};
DecimalFormat df = new DecimalFormat("###.0");
for (double d : dArr) {
    System.out.printf("%.16f %s%n", d, df.format(d));
}
显示在Java中,给定的双精度值是精确的,舍入模式也是半到偶数

5.0500000000000000 5.0
5.1500000000000000 5.2
5.2500000000000000 5.2
5.3500000000000000 5.4
5.4500000000000000 5.4
5.5500000000000000 5.6

我已经删除了我的解决方案,因为它是错误的。但是我会解释C++的四舍五入的不太明显的行为。

使用此C++代码揭示双倍的内部价值。

int main() {
    char result[20];
    double dArr [6] = {5.05, 5.15, 5.25, 5.35, 5.45, 5.55};
    for(int i = 0; i< 6; i++) {
        sprintf(result, "%3.16f  %3.1lf", dArr[i], dArr[i]);
        cout << result << endl;
    }
    return 0;
}
我最初的建议得到修正

double[] dArr = {5.05, 5.15, 5.25, 5.35, 5.45, 5.55};
DecimalFormat df = new DecimalFormat("###.0");
for (double d : dArr) {
    System.out.printf("%.16f %s%n", d, df.format(d));
}
显示在Java中,给定的双精度值是精确的,舍入模式也是半到偶数

5.0500000000000000 5.0
5.1500000000000000 5.2
5.2500000000000000 5.2
5.3500000000000000 5.4
5.4500000000000000 5.4
5.5500000000000000 5.6
Java中的等效语言:

System.out.printf("%3.1f", 5.05); // 5.1
Java中的等效语言:

System.out.printf("%3.1f", 5.05); // 5.1

如果您想使用自己的舍入:

import java.math.BigDecimal;

public class BigDecimalFormat
{
   private static int ROUNDING_MODE = BigDecimal.ROUND_HALF_EVEN;
   private static int DECIMALS = 1;

  public static void main(String[] args) {
      double[] dArr = {5.05, 5.15, 5.25, 5.35, 5.45, 5.55};
      for (double d : dArr)
        System.out.println(rounded(new BigDecimal(d)));
  }   

   public static BigDecimal rounded(BigDecimal number){
    return number.setScale(DECIMALS, ROUNDING_MODE);
  }
}
您似乎要设置货币编号的格式:

public static String currencyFormat(BigDecimal n) {
  return NumberFormat.getCurrencyInstance().format(n);
}

如果您想使用自己的舍入:

import java.math.BigDecimal;

public class BigDecimalFormat
{
   private static int ROUNDING_MODE = BigDecimal.ROUND_HALF_EVEN;
   private static int DECIMALS = 1;

  public static void main(String[] args) {
      double[] dArr = {5.05, 5.15, 5.25, 5.35, 5.45, 5.55};
      for (double d : dArr)
        System.out.println(rounded(new BigDecimal(d)));
  }   

   public static BigDecimal rounded(BigDecimal number){
    return number.setScale(DECIMALS, ROUNDING_MODE);
  }
}
您似乎要设置货币编号的格式:

public static String currencyFormat(BigDecimal n) {
  return NumberFormat.getCurrencyInstance().format(n);
}
自动四舍五入到最接近的十分之一

如果要将其舍入为的小数位数更改为:

“%.1f”
四舍五入到一个位置
“%.2f”
四舍五入到两个位置
“%.3f”
四舍五入到三个位置。。。等等

System.out.printf("%.1f", dArr[i]);
取代:

sprintf(result,"%3.1lf",dArr[i]);
cout << result << endl;
sprintf(结果,“%3.1lf”,dArr[i]);
库特
自动四舍五入到最接近的十分之一

如果要将其舍入为的小数位数更改为:

“%.1f”
四舍五入到一个位置
“%.2f”
四舍五入到两个位置
“%.3f”
四舍五入到三个位置。。。等等

System.out.printf("%.1f", dArr[i]);
取代:

sprintf(result,"%3.1lf",dArr[i]);
cout << result << endl;
sprintf(结果,“%3.1lf”,dArr[i]);


这个问题是否清楚地提到了
BigDecimal.
你没有。@EJP我假设一旦你知道如何做,你就会知道如何用BigDecimal来做转换总是用“四舍五入”来做。请看问题是否清楚地提到了
BigDecimal.
你没有。@EJP我假设一旦你知道如何做,你会知道如何用大分贝来做,转换总是用“取整一半”来完成的。看,在回顾了我自己的答案后,我投票赞成你的答案。;-)多谢各位。我的理解是,四舍五入甚至应该四舍五入到最接近的偶数。因此,四舍五入(5.35)=5.4;&&四舍五入(5.45)==5.4基本上区别在于:BigDecimal(5.55)==5.499999。。。四舍五入到5.5;BigDecimal(“5.55”)= 5.55,回合到5.6。你喜欢java类如何与C++?java 100次比较容易和更干净。我投票赞成你的答案,在审查我自己的。多谢各位。我的理解是,四舍五入甚至应该四舍五入到最接近的偶数。因此,四舍五入(5.35)=5.4;&&四舍五入(5.45)==5.4基本上区别在于:BigDecimal(5.55)==5.499999。。。四舍五入到5.5;BigDecimal(“5.55”)=5.55,回合到5.6。你喜欢java类如何与C++?java 100次比较容易和更干净。转换总是用“回合半向上”来完成。