怎样让Edit方框里显示的 数字的小数 是固定的?

kittyhust 2006-05-10 08:35:37
怎样让Edit方框里显示的 数字的小数 是固定的?
程序运行后用于输出结果的Edit 方框里面的小数位数实在太多.很不美观

可以通过修改 Edit 的设置 或其他语句 让输出的小数位数为固定吗?

最好不修改运算部分以前的....怕影响精度
...全文
256 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
daydayup234 2006-05-11
  • 打赏
  • 举报
回复
这样解决了:
事先放个
TEdit *Edit1;
TMemo *Memo1;

当焦点移开Edit1时:

void __fastcall TForm1::Edit1Exit(TObject *Sender)
{
try
{
double f;
f=Edit1->Text.ToDouble();
Edit1->Text=Format("%8.2f", ARRAYOFCONST((f)));
}
catch(...)
{
Edit1->Text=" ";
ShowMessage("输入不是数字可能");
}
}
jiangshx 2006-05-11
  • 打赏
  • 举报
回复
楼主试了FormatFloat吗,
Edit->Text=FormatFloat(".000",FloatValue);
可以输出整数部分的。
CACACACACA 2006-05-11
  • 打赏
  • 举报
回复
如果只是控制小数位数,
Edit->Text=FormatFloat("#.000",FloatValue);
BlueDeepOcean 2006-05-11
  • 打赏
  • 举报
回复
另外一个简单的组件是MaskEdit,可通过设置!9999.999;0; 等方式确定小数位数,但是不具四舍五入的功能。
BlueDeepOcean 2006-05-11
  • 打赏
  • 举报
回复
因此,我觉得还是FormatFloat()比较好。
BlueDeepOcean 2006-05-11
  • 打赏
  • 举报
回复
Formats the floating-point value given by value using the format string given by format.

static AnsiString __fastcall FormatFloat(const AnsiString& format,const long double& value);

Description

The following format specifiers are supported in the format string:

Specifier Represents

0 Digit place holder. If the value being formatted has a digit in the position where the '0' appears in the format string, then that digit is copied to the output string. Otherwise, a '0' is stored in that position in the output string.
# Digit placeholder. If the value being formatted has a digit in the position where the '#' appears in the format string, then that digit is copied to the output string. Otherwise, nothing is stored in that position in the output string.

. Decimal point. The first dot character ('.') in the format string determines the location of the decimal separator in the formatted value; any additional dot characters are ignored. The actual character used as the decimal separator in the output string is determined by the DecimalSeparator global variable, whose default value is specified in the Number Format of the International section in the Windows Control Panel.
, Thousand separator. If the format string contains one or more comma characters (','), the output will have thousand separators inserted between each group of three digits to the left of the decimal point. The placement and number of comma characters in the format string does not affect the output, except to indicate that thousand separators are wanted. The actual character used as the thousand separator in the output is determined by the ThousandSeparator global variable, whose default value is specified in the Number Format of the International section in the Windows Control Panel.

E+ Scientific notation. If any of the strings 'E+', 'E-', 'e+', or 'e-' are contained in the format string, the number is formatted using scientific notation. A group of up to four '0' characters can immediately follow the 'E+', 'E-', 'e+', or 'e-' to determine the minimum number of digits in the exponent. The 'E+' and 'e+' formats cause a plus sign to be output for positive exponents and a minus sign to be output for negative exponents. The 'E-' and 'e-' formats output a sign character only for negative exponents.

'xx'/"xx" Characters enclosed in single or double quotes are output as-is, and do not affect formatting.
; Separates sections for positive, negative, and zero numbers in the format string.

The locations of the leftmost '0' before the decimal point in the format string and the rightmost '0' after the decimal point in the format string determine the range of digits that are always present in the output string.

The number being formatted is always rounded to as many decimal places as there are digit placeholders ('0' or '#') to the right of the decimal point. If the format string contains no decimal point, the value being formatted is rounded to the nearest whole number.

If the number being formatted has more digits to the left of the decimal separator than there are digit placeholders to the left of the '.' character in the format string, the extra digits are output before the first digit placeholder.

To allow different formats for positive, negative, and zero values, the format string can contain from one to three sections separated by semicolons.

One section: The format string applies to all values.
Two sections: The first section applies to positive values and zeros, and the second section applies to negative values.
Three sections: The first section applies to positive values, the second applies to negative values, and the third applies to zeros.

If no formatting is specified for positive values, positive values are formatted using general floating-point formatting with 15 significant digits, corresponding to a call to FloatToStrF with the sffGeneral format. General floating-point formatting is also used if the value has more than 18 digits to the left of the decimal point and the format string does not specify scientific notation.

The following table shows some sample formats and the results produced when the formats are applied to different values:

Format string- 1234 -1234 0.5 0

1234 -1234 0.5 0
0 1234 -1234 1 0
0.00 1234.00 -1234.00 0.50 0.00
#.## 1234 -1234 .5
#,##0.00 1,234.00 -1,234.00 0.50 0.00
#,##0.00;(#,##0.00) 1,234.00 (1,234.00) 0.50 0.00
#,##0.00;;Zero 1,234.00 -1,234.00 0.50 Zero
0.000E+00 1.234E+03 -1.234E+03 5.000E-01 0.000E+00
#.###E-0 1.234E3 -1.234E3 5E-1 0E0
lykey 2006-05-11
  • 打赏
  • 举报
回复
double a=12.3236;

Edit1->Text=FloatToStrF(a,ffFixed,15,3);

查一下FloatToStrF的帮助,其中最后的参数3,表示小数点后有几位,如果2位,就写字。
该函数将自动四舍五入
Edit1中显示12.324
kittyhust 2006-05-10
  • 打赏
  • 举报
回复
谢谢大家的回帖,我试了下
1.帮助里有ROUNDTO(fltVal,-n) 这函数的确是非常好的,很切合我的需要,但是却运行不了,系统提示找不到这函数.我在inlclude文件夹打开math.h文件看了下.果然没有round 和roundto.但帮助里有.这该怎么办呢?我也打过一个补丁,为什么没那函数,怎么解决?可以下完成的math.h吗?
2.CEIL/floor我看了下帮助文挡,不大懂...好象是四舍五入,但只保留整数.是不是这样?该怎么用啊
3.FormatFloat函数可以很好的解决小数部分问题.如果按上面的格式0.123456就变成.123了,整数部分没有.如果是"000.000"格式,那就变成000.123,这也是不可取的.
jiangshx 2006-05-10
  • 打赏
  • 举报
回复
不好意思,上面是随手写的,有误,
Edit->Text=FormatFloat(".000",FloatValue);
jiangshx 2006-05-10
  • 打赏
  • 举报
回复
Edit->Text=FormatFloat(".000","0.1236");
stevenjscn 2006-05-10
  • 打赏
  • 举报
回复
如果用的是DBEDIT,连到数据集里,则可对数据集里的FIELD的DISPLAY FORMAT设置“#.00”表示为两位小数。
当然,如果是EDIT,则,可用ROUNDTO(fltVal,-n)将其四舍五入。或者用CEIL/floor等函数
kittyhust 2006-05-10
  • 打赏
  • 举报
回复
控制小数部分的位数.
kittyhust 2006-05-10
  • 打赏
  • 举报
回复
控制小数本分的位数.
CACACACACA 2006-05-10
  • 打赏
  • 举报
回复
不是很明白楼主的意思,是想控制小数点在第几位,还是要控制小数的位数.
MaskEdit1可以控制输入的格式.
如果EDIT的话,可以在ONCHANGE中写代码控制一下.

13,825

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧