简体   繁体   English

如何使用“平均”公式限制Excel单元格中的小数位数?

[英]How can I restrict the decimal count in an Excel cell with an “Average” formula?

I'm using C# Excel Interop to create spreadsheets. 我正在使用C#Excel Interop创建电子表格。 I'm using formulas for a "totals" section at the bottom of a sheet. 我在工作表底部的“总计”部分使用公式。 Here's the code I'm using for that: 这是我正在使用的代码:

var totalTotalOrdersCell = (Range)_xlSheetDelPerf.Cells[curDelPerfRow + 2, TOTAL_ORDERS_COLUMN];
totalTotalOrdersCell.Formula = string.Format("=SUM(J10:J{0})", curDelPerfRow);

var avgAvgWeeklyDeliveriesCell = (Range)_xlSheetDelPerf.Cells[curDelPerfRow + 2, AVG_WEEKLY_DELIVERIES_COLUMN];
avgAvgWeeklyDeliveriesCell.Formula = string.Format("=AVERAGE(K10:K{0})", curDelPerfRow);

var avgAvgOrderAmountCell = (Range)_xlSheetDelPerf.Cells[curDelPerfRow + 2, AVG_ORDER_AMOUNT_COLUMN];
avgAvgOrderAmountCell.Formula = string.Format("=AVERAGE(L10:L{0})", curDelPerfRow);

var avgAvgPackageCountCell = (Range)_xlSheetDelPerf.Cells[curDelPerfRow + 2, AVG_PACKAGE_AMOUNT_COLUMN];
avgAvgPackageCountCell.Formula = string.Format("=AVERAGE(M10:M{0})", curDelPerfRow);

var totalTotalSalesCell = (Range)_xlSheetDelPerf.Cells[curDelPerfRow + 2, TOTAL_SALES_COLUMN];
totalTotalSalesCell.Formula = string.Format("=SUM(N10:N{0})", curDelPerfRow);

var totalTotalPackageCountCell = (Range)_xlSheetDelPerf.Cells[curDelPerfRow + 2, TOTAL_PACKAGE_COUNT_COLUMN];
totalTotalPackageCountCell.Formula = string.Format("=SUM(O10:O{0})", curDelPerfRow);

This is what is being produced: 这是产生的:

在此处输入图片说明

The monetary/currency vals are formatting just right, without any intervention from me - Excel is apparently smart about money. 货币/货币汇率格式正确,没有我的任何干预-Excel显然对金钱很精明。

As to the integers, though, not so much - it shows "20192" instead of "20,192" - but I already asked about that particular issue here . 至于整数,虽然不是很多-它显示的是“ 20192”而不是“ 20,192”-但是我已经在这里询问了该特定问题。

My question now is - and it's a "bigger deal" - how can I restrict the decimal count on the averaged values? 现在我的问题是-这是一个“更大的交易”-如何限制平均值的小数位数? I want "15.23" not "15.23076923" and "34.17" not the much longer and more precise version of the value. 我希望“ 15.23”而不是“ 15.23076923”和“ 34.17”不是该值的更长,更精确的版本。

How can I tell the Formula former that two decimal points is enough? 我怎么能告诉公式公式制作者两个小数点就足够了?

Have you tried the =TEXT formula? 您是否尝试过=TEXT公式? - syntax is like this: =TEXT("412.24134","#,###.00") and the answer will be displayed as 412.24 : -语法如下: =TEXT("412.24134","#,###.00")并且答案将显示为412.24

so wrt your code, something like this (has not been tested): 因此,您的代码是这样的(未经测试):

var avgAvgWeeklyDeliveriesCell = (Range)_xlSheetDelPerf.Cells[curDelPerfRow + 2, AVG_WEEKLY_DELIVERIES_COLUMN];
avgAvgWeeklyDeliveriesCell.Formula = string.Format("=TEXT(AVERAGE(K10:K{0})", curDelPerfRow),"#,###.00");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM