如何在SAP Crystal Reports中舍入并去除小数位?附公式咨询
Hey there! It looks like your two questions are targeting the same core goal—taking numeric values, rounding them to the nearest whole number, and stripping off any decimal places. Let's break this down using your existing formula as a starting point.
Key Concepts to Know
First, SAP Crystal Reports has two straightforward ways to handle this:
- Using Format Strings: The
CStr()function with a format specifier like"0"automatically rounds numbers to the nearest integer and removes decimals when converting to a string. - Using the
Round()Function: This explicitly rounds a numeric value to a specified number of decimal places (in your case,0for whole numbers), which you can then convert to a string.
Adjusting Your Existing Formula
Your current formula is:
WhilePrintingRecords; NumberVar ItemNumber; CStr(ItemNumber, "0") & "/" & CStr(Count({rpt_PackingSlip.LabelQTY}, {rpt_PackingSlip.WorkOrderNo}))
Here are two refined versions that ensure both values are rounded to whole numbers and have decimals removed:
Version 1: Using Format Strings (Simpler)
This leans into the built-in behavior of CStr() to handle rounding and decimal removal in one step, even for the Count() result (in case your aggregation ever returns a non-integer value):
WhilePrintingRecords; NumberVar ItemNumber; CStr(ItemNumber, "0") & "/" & CStr(Count({rpt_PackingSlip.LabelQTY}, {rpt_PackingSlip.WorkOrderNo}), "0")
Version 2: Using Round() (Explicit & Clear)
If you want to make the rounding logic explicit (great for readability or if you might adjust decimal places later), use the Round() function before converting to a string:
WhilePrintingRecords; NumberVar ItemNumber; CStr(Round(ItemNumber, 0)) & "/" & CStr(Round(Count({rpt_PackingSlip.LabelQTY}, {rpt_PackingSlip.WorkOrderNo}), 0))
Quick Note
Both methods will round values like 1.4 to 1 and 1.6 to 2, which matches your request to round to the nearest integer while removing decimals. If you ever needed to truncate decimals instead of rounding (e.g., turn 1.9 into 1), you'd use the Int() function—but that's not what you're asking for here.
内容的提问来源于stack exchange,提问作者user9399808

