Format specifications for real numbers
15,320
Using %7
works for your values with 2 simulators I tried:
module tb;
real expected, actual;
initial begin
expected = 12.25; actual= 12.75;
$display("expected= %7.2f, actual= %7.2f", expected, actual);
expected= 4093.25; actual= 4094.75;
$display("expected= %7.2f, actual= %7.2f", expected, actual);
#5 $finish;
end
endmodule
Output:
expected= 12.25, actual= 12.75
expected= 4093.25, actual= 4094.75
Author by
nguthrie
Updated on June 27, 2022Comments
-
nguthrie almost 2 years
I would like to print some real numbers to a log file. To make them easy to read I would like them to all have the same width. I know these numbers will range from 0 to 4095.75 so I tried this:
$display("expected= %4.2f, actual= %4.2f", expected, actual)
What I expected to see was this:
expected= 12.25, actual= 12.75 expected= 4093.25, actual= 4094.75
But what I got in was this:
expected= 12.25, actual= 12.75 expected= 4093.25, actual= 4094.75
How do I force the width of the value above the decimal point to be 4 characters? Section 21.2.1.3 Size of displayed data of the LRM is silent on %f.