Counting the number of decimal places in pascal -


i started studying pascal , have pascal program homework. made don't know how count number of decimal places in real number (the number of digit after ".").

i need format real number (like write(real:0:dec) dec number of decimal digit don't know how know). i'd because don't want in scientific notation or many unnecessary zeros.

for example if real number 1.51 (x) , write writeln(x:0:4); or writeln(format('%*.*f', [0, 4, x])); show 1.5100 want 1.51; , if number 1.513436, show 1.5134 . make writeln(x:0:dec); makes dec number of decimal digits of x.

the format() function used in situations this.

writeln(format('%*.*f', [0, dec, your_real_number])); 

*.* interpreted total_characters.decimal_digits. passing 0 first means width adjusted according how large real is. number of decimals can variable (dec), can adjust specification.


update:

you mention want exact representation of float respect number of decimals.

as mentioned in comment, floating point values not have finite number of decimals. , decimal fractions cannot represented binary type.

there libraries can handle floating point values of arbitrary size. see tbigfloat example. there formatting routine can strip redundant zeroes decimal float.


still, there possibility remove trailing zeroes using general format specifier:

writeln(format('%g',[your_real_number])); 

you can specify width , number of significant digits well.


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -