如何在Delphi中将浮点值显示为科学参数

发布于 2024-10-17 02:47:05 字数 135 浏览 6 评论 0原文

我们经常需要以带有乘数和单位的科学形式显示浮点值,例如 1500 V(伏特)的值将显示为 1.5 kV。 1e-4 V 的非常小的电压将显示为 100 uV。多年来,我们一直使用内部创建的例程来进行浮点到字符串的转换,但我最近想知道这种方法是否更普遍可用?

We have a frequent need to display floating point values in a scientific form with multiplier and units, for example the value of 1500 V (volts) would be displayed as 1.5 kV.
A very small voltage of 1e-4 V would be displayed as 100 uV. For years we've used an internally created routine to make this float to string conversion but I was minded to wonder recently whether such a means was more generally available?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

○愚か者の日 2024-10-24 02:47:05

我正在使用这个 FormathWithPrefix 函数,但对对数知之甚少...:)

支持 SI 前缀 范围!

function FormathWithPrefix(n: double; decimals: integer): string;
var
  index: integer;
const
  Prefixes: array[-9..9]of string = ('<', 'y', 'z', 'a', 'f', 'p', 'n', 'µ', 'm', '',
    'k','M', 'G', 'T', 'P', 'E', 'Z', 'Y', '>');   
begin
  index := round((Ln(n) / Ln(10) - 1) / 3);
  if index > 9 then 
    index := 9;
  if index < -9 then 
    index := -9;
  result := (FloatToStrF(n / Exp(index * 3 * ln(10)) , ffFixed, 20, decimals) + Prefixes[index]);
end;

begin
  n := 1500;
  Writeln(FormathWithPrefix(n, 1),'V');

I'm using this FormathWithPrefix function with little knowledge about logarithms... :)

Support SI prefixes range!

function FormathWithPrefix(n: double; decimals: integer): string;
var
  index: integer;
const
  Prefixes: array[-9..9]of string = ('<', 'y', 'z', 'a', 'f', 'p', 'n', 'µ', 'm', '',
    'k','M', 'G', 'T', 'P', 'E', 'Z', 'Y', '>');   
begin
  index := round((Ln(n) / Ln(10) - 1) / 3);
  if index > 9 then 
    index := 9;
  if index < -9 then 
    index := -9;
  result := (FloatToStrF(n / Exp(index * 3 * ln(10)) , ffFixed, 20, decimals) + Prefixes[index]);
end;

begin
  n := 1500;
  Writeln(FormathWithPrefix(n, 1),'V');
南街女流氓 2024-10-24 02:47:05

如果您的目标是使您的例程更加“原生”,您可以查看 ConvUtils 和 StdConvs 单元,看看是否可以将您的例程基于这些单元提供的转换函数。 (不确定这些单元是什么时候引入的。快速谷歌搜索建议使用 Delphi 6)

但这不一定会给您带来多大帮助。 (取决于您想要实现的目标)

If your goal is to make your routines more "native", you could take a look at the ConvUtils and StdConvs units and see if you could base your routines on the conversion functions offered by these units. (Not sure when those units were introduced. Quick google search suggest Delphi 6)

But that won't necessarily achieve much for you. (Depending on what you are looking to achieve)

泅人 2024-10-24 02:47:05

在这里试试:http://www.delphibasics.co.uk/RTL.asp? Name=FloatToStrF

特别是这个: `ShowMessage('Using 8,4 = '+FloatToStrF(amount1, ffFixed, 8, 4));
会表明你可以使用这样的东西:

var 
  Answer:string;    
  thevolts:double;
begin
  Answer:= FloatToStrF(thevolts, ffFixed, 8, 4)+' kV'
end;

Try here: http://www.delphibasics.co.uk/RTL.asp?Name=FloatToStrF

Particularly this one: `ShowMessage('Using 8,4 = '+FloatToStrF(amount1, ffFixed, 8, 4));
would indicate you can use something like this:

var 
  Answer:string;    
  thevolts:double;
begin
  Answer:= FloatToStrF(thevolts, ffFixed, 8, 4)+' kV'
end;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文