C ++长双重格式而不舍入

时间:2017-04-18 22:10:02

标签: c++ double

我想从长双精度显示两个小数位而不进行舍入。

示例:

14.999999 => 14.99

12.501 => 12.50

12.505 => 12.50

某些值存在问题。

例如:

例外204969/340 = 602.85

我的结果204969/340 = 602.84

代码示例:

long long a = 204969;
long long b = 340;
long double final_result = (long double) a / (long double) b;
final_result = ((long long)(final_result * 100) / 100.00);

cout << fixed << setprecision(2) <<final_result;

为什么?

4 个答案:

答案 0 :(得分:3)

问题在于204969/340,当表示为双精度时,并不完全是602.85,而是602.849999999999999978,此值100 60284.9999999999999978long long ,此值转换为60284得到100.00,此除以602.840000000000031832得到602.84,然后以精度2舍入,最后打印long

我做的是&#34;次100&#34;在100.0的基础上,最后除以long double final_result = (a*100/b)/100.00;

602.850000000000022737

然后给出602.85,并舍入到精度2打印import java.math.BigDecimal; import java.util.Comparator; import java.util.Date; /** * @author * */ public class Annuals implements Comparable<Annuals> { private BigDecimal value; private Date dateCalculated; private BigDecimal dataYear; private String type; /** * @return the value */ public BigDecimal getValue() { return value; } /** * @param value the value to set */ public void setValue(BigDecimal value) { this.value = value; } /** * @return the dateCalculated */ public Date getDateCalculated() { return dateCalculated; } /** * @param dateCalculated the dateCalculated to set */ public void setDateCalculated(Date dateCalculated) { this.dateCalculated = dateCalculated; } /** * @return the dataYear */ public BigDecimal getDataYear() { return dataYear; } /** * @param dataYear the dataYear to set */ public void setDataYear(BigDecimal dataYear) { this.dataYear = dataYear; } /** * @return the type */ public String getType() { return type; } /** * @param type the type to set */ public void setType(String type) { this.type = type; } /* (non-Javadoc) * @see java.lang.Object#toString() */ @Override public String toString() { return "Annuals [value=" + value + ", dateCalculated=" + dateCalculated + ", dataYear=" + dataYear + ", type=" + type + "]"; } public static Comparator<Annuals> yearComparator = new Comparator<Annuals>() { @Override public int compare(Annuals o1, Annuals o2) { return o1.getDataYear().compareTo(o2.getDataYear()); } }; @Override public int compareTo(Annuals o) { return this.value.compareTo(o.value); } }

答案 1 :(得分:0)

这是二进制表示的本质。您要求小数位的结果,但浮点数 小数位。它有二进制位置,这两个是不可通约的。如果你想要小数位,请使用十进制基数,即在标题中执行的操作和格式双精度,使用掩码进入char缓冲区。

乘以然后除以100 的技术不起作用。有关证据,请参阅here

答案 2 :(得分:-1)

可能是与长双重相关的差异错误

我试过这段代码:

long long a = 204969;
long long b = 340;

double final_result = (double)(a*100/b)/100;

它给了我:602.85。

如果您只是为了输入结果而需要它,您可以不使用双打来实现:

 long long result = a*100/b;
 printf ("Result = %ld.%ld\n", result/100, result % 100); 

答案 3 :(得分:-2)

  

final_result 仍然 long double 时使用 floor 功能,以便小数位清除然后转换为 long long

  long long a = 204969;
  long long b = 340;
  long double final_result = (long double) a / (long double) b;
  final_result = ((long long)(floor(final_result * 100)) / 100.00);
  cout << fixed << setprecision(2) <<final_result;