Counting the number of decimal places in pascal

后端 未结 3 1612
南方客
南方客 2021-01-15 19:40

I just started studying pascal and I have to do a pascal program as homework. I made it but I don\'t know how to count the number of decimal places in a real number<

3条回答
  •  醉话见心
    2021-01-15 20:38

    For example, if you have input x=1.51 in real variable type, then you write only writeln(x), the output will be 1.5100000000. If you write writeln(x:0:3), the output will be 1.510 (3 digits after ".") ...

    var x: real;
    Begin
    x:=1.51;
    writeln(x); //output 1.5100000000
    writeln(x:0:4); //output 1.5100 (4 digits after ".")
    writeln(x:0:2); //output 1.51 (2 digits after ".")
    readln;
    End.
    

    From your other example, if your input is 1.512426, with writeln(x:0:5) it will only show 5 digits after "." and the output will be 1.51242

    var x: real;
    Begin
    x:=1.512426;
    writeln(x); //output 1.5124260000
    writeln(x:0:4); //output 1.5124 (4 digits after ".")
    writeln(x:0:2); //output 1.51 (2 digits after ".")
    readln;
    End. 
    

    So, if you write writeln(x:0:dec) the output will be "dec" digits after "."

    Hope this helps, I'm just trying to answer from a different perspective.

提交回复
热议问题