Reputation: 23
Can I define a round function in gnuplot that automatically rounds numbers not to a close integer (like floor etc.) but to the leading digit, like
round(0.00230354) = 0.002 ?
Of course, I could multiply every value by the correct power of 10 and then floor it. But in order to do so I would need to look at every number individually. I would want to use the function to label color bars where I determine the tics positions dynamically with the stats command for varying datasets.
Upvotes: 2
Views: 2078
Reputation: 13097
you could most likely use sprintf
for this task (i.e., to obtain first the "rounded" string representation of the number of interest which is then converted back to a number via the real
function):
gnuplot> x = real(sprintf("%.0E", 0.00230354));print x
0.002
gnuplot> x = real(sprintf("%.0E", 0.00260354));print x
0.003
So in terms of a custom function round
, the example would look like:
gnuplot> round(x) = real(sprintf("%.0E", x));
gnuplot> print round(0.00230354);
0.002
Or if you wanted just the string representation which you could then use directly for the labels then perhaps the g/G
conversion specifier might be more relevant:
gnuplot> s = sprintf("%.1g", 0.00230354); print s
0.002
gnuplot> s = sprintf("%.1g", 0.000000230354); print s
2e-07
gnuplot> s = sprintf("%.1g", 0.000000260354); print s
3e-07
Upvotes: 3