1

我有一个包含类型字段的表numeric(28,10)。我想用等宽字体和匹配的小数位数显示记录,以便它们在右对齐时对齐。

有什么方法可以计算出在给定结果集中可以找到的最大小数位数,以便我可以在显示每条记录时进行相应的格式化?

编辑:对不起,我应该更清楚......如果结果集包含只有 3 个小数位的数字,那么所有数字都应该只有 3 个小数位(用零填充)。

4

2 回答 2

1

等宽字体完全是一个演示问题......

我在测试时看不到您需要正确对齐:

CREATE TABLE [dbo].[Table_1](
  [num] [numeric](28, 10) NOT NULL
)

INSERT INTO [example].[dbo].[Table_1] VALUES (1.1234567890);
INSERT INTO [example].[dbo].[Table_1] VALUES (1.123456789);
INSERT INTO [example].[dbo].[Table_1] VALUES (1.1234567);

SELECT [num]
  FROM [example].[dbo].[Table_1]

...返回:

num
---------------
1.1234567890
1.1234567890
1.1234567000

所以问题是——你想做什么却没有给你想要的输出?

于 2010-09-26T23:48:43.170 回答
0

你想在哪里显示结果?查询分析器?在应用程序中?

你可以

 a) format the column to have a
    finite number (known in advance) of
    digits to the right of the decimal
    point, truncating at that position;
    this is the typical practice or

 b)    read through all of the rows in the
    resultset to determine the value
    with the greatest number of digits
    to the right of the decimal point 
    (casting to string, splitting the
    value using the decimal point as
    delimiter, and getting the length of
    the decimal-fraction string)

如果由于某种原因选项 a) 是不可接受的,那么您将不得不做 b) 在程序上,无论是存储过程中的服务器端还是客户端程序中的客户端。

于 2010-09-26T23:45:27.077 回答