ArticleDirectory
- Requirement Overview
- Implementation
Requirement Overview
In some statistical reports, the value range of a certain number is quite large. In order to reduce the total number of digits displayed for a large data, a small data must be displayed with sufficient precision, that is to say, different decimal places are displayed based on the data size, which is similar to the floating point mechanism that represents the decimal data in the computer.
You can set the "format" attribute of data to display numbers in the specified format in a report. However, the preceding requirements cannot be achieved directly by setting the format, instead, we need to use the report Script Function.
Implementation
First, set the data format to the maximum number of decimal places required by the output. For example, if the format is "#, #0.000000", the data is always displayed with 5 decimal places. Then, use the report script to determine the decimal places to be output based on the data value size, and cut off the remaining decimal places to obtain the expected explicit text.
To write a report script on the "get show text script" attribute in the field or statistical box, you can set its display text. example scriptCode:
// Determine the number of digits to be displayed based on the Value
VaR v = sender. value;
VaR digit = 0;
If (V> 2000)
Digit = 0;
Else if (v> 1500)
Digit = 2;
Else if (v> 800)
Digit = 3;
Else
Digit = 5;
VaR text = sender. displaytext; // obtain the plain text generated based on the formatted string, for example, 123.45600
VaR Index = text. indexof ("."); // you can specify the decimal point.
If (digit> 0)
Index = index + digit + 1;
Else
Index = index;
Sender. displaytext = text. substr (0, index); // intercept the displayed text and set it to the displayed Text of the data.
Sample download (http://www.rubylong.cn/download/samples/FloatDecimal.grf)