How to display a floating point value as a scientific parameter in Delphi

We often have to display floating point values ​​in scientific form with a multiplier and units, for example, a value of 1500 V (volts) will be displayed as 1.5 kV. A very small voltage of 1e-4 V will be displayed as 100 μV. For many years, we used an internally created procedure to convert this float to a string conversion, but I was very interested to know if such a tool was more widely available?

+4
source share
3 answers

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

Support SI prefixes .

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'); 
+4
source

If your goal is to make your routines more “native”, you can take a look at the ConvUtils and StdConvs blocks and see if you can base your routines on the conversion functions offered by these devices. (Not sure when these devices were introduced. A quick Google search suggests Delphi 6)

But it does not necessarily bring you much. (Depending on what you want to achieve)

+1
source

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

In particular, it is: `ShowMessage ('Using 8.4 =' + FloatToStrF (amount1, ffFixed, 8, 4)); that you can use something like this:

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

Source: https://habr.com/ru/post/1339655/


All Articles