[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: numerical values
From: |
Przemek Klosowski |
Subject: |
Re: numerical values |
Date: |
Thu, 20 Mar 2008 09:47:20 -0400 (EDT) |
As Ben pointed out, printf formats easily control the
number of decimal places, as shown above. It is even
possible to provide that number at runtime:
sprintf('%7.*f', 2, 123.4567) --> 123.46
sprintf('%7.*f', 3, 123.4567) --> 123.457
Limiting the total number of decimal places is trickier: the printf
formats don't seem to allow this control you could use the exponential
notation which normalizes to range [1,9) but then the numbers will
always be written in the exponent notation. You can manipulate
the resulting string, though:
sprintf ('%f\n', 123.45678)(1:5)
Note that
disp (sprintf ('Three decimal places: %7.3f', 123.45678)
can be simply written as
printf ('Three decimal places: %7.3f\n', 123.45678)