我有一个包含几个元素的数组:
MSN = 34.3433423432434%
Chrome = 12.4343434353534%
Gtalk = 32.23233543543532%
...
我将此数组作为 y 轴标签传递,以与名为 的模块一起使用GD::图。我现在面临的问题是图表上的数字太大,以至于它们与相邻条目重叠并使其无法读取。
有没有一种方法可以将数组中的所有元素四舍五入到小数点后两位?并将其设为xx.xx%?
<子><上>
另外,任何熟悉使用 GD::Graph 的人,你知道如何增加图表上的文本大小吗?我可以很好地增加标题/图例的大小,但“Gtalk”或“32.23233543543532%”中的实际文本非常小,我已经尝试了 http://search.cpan.org/dist/GDGraph/Graph.pm,但它们似乎不适合我!
I have an array with a few elements:
MSN = 34.3433423432434%
Chrome = 12.4343434353534%
Gtalk = 32.23233543543532%
...
And I'm passing this array as y-axis labels to use with a module called GD::Graph. The problem I am facing right now, is that the number are so big on the graph that they overlap with the adjacent entry and make it unreadable.
Is there a way where I can round off ALL the elements in an array to just 2 decimal places? And make it xx.xx%?
Also, anyone familar with using GD::Graph, do you know how can I increase the text-size on the graph? I can increase Title / Legend size fine, but the actual text as in 'Gtalk' or '32.23233543543532%' is really small and I've tried a lot of the commands from http://search.cpan.org/dist/GDGraph/Graph.pm, but they don't seem to work for me!
发布评论
评论(3)
来自 perlfaq4 的回答 Perl 有 round() 函数吗? ceil() 和 Floor() 怎么样?三角函数?
:
请记住,int() 只是向 0 截断。要舍入到特定位数,sprintf() 或 printf() 通常是最简单的方法。
POSIX 模块(标准 Perl 发行版的一部分)实现 ceil()、floor() 以及许多其他数学和三角函数。
在 5.000 到 5.003 perls 中,三角函数是在 Math::Complex 模块中完成的。在 5.004 中,Math::Trig 模块(标准 Perl 发行版的一部分)实现了三角函数。它在内部使用 Math::Complex 模块,某些函数可以从实轴突破到复平面,例如 2 的反正弦。
金融应用中的舍入可能会产生严重影响,应精确指定所使用的舍入方法。在这些情况下,最好不要相信 Perl 使用的系统舍入,而是自己实现所需的舍入函数。
要了解原因,请注意在中途点交替方面仍然存在问题:
不要责怪 Perl。这和 C 中的一样。IEEE 说我们必须这样做。绝对值为 2**31 以下整数(在 32 位机器上)的 Perl 数字的工作方式与数学整数非常相似。其他数字不保证。
From perlfaq4's answer to Does Perl have a round() function? What about ceil() and floor()? Trig functions?
:
Remember that int() merely truncates toward 0. For rounding to a certain number of digits, sprintf() or printf() is usually the easiest route.
The POSIX module (part of the standard Perl distribution) implements ceil(), floor(), and a number of other mathematical and trigonometric functions.
In 5.000 to 5.003 perls, trigonometry was done in the Math::Complex module. With 5.004, the Math::Trig module (part of the standard Perl distribution) implements the trigonometric functions. Internally it uses the Math::Complex module and some functions can break out from the real axis into the complex plane, for example the inverse sine of 2.
Rounding in financial applications can have serious implications, and the rounding method used should be specified precisely. In these cases, it probably pays not to trust whichever system rounding is being used by Perl, but to instead implement the rounding function you need yourself.
To see why, notice how you'll still have an issue on half-way-point alternation:
Don't blame Perl. It's the same as in C. IEEE says we have to do this. Perl numbers whose absolute values are integers under 2**31 (on 32 bit machines) will work pretty much like mathematical integers. Other numbers are not guaranteed.
输出:
如果您想按特定顺序提取值,请使用哈希切片:
或者,如果您只想在
plot
调用中排列键和值:注意:要增加文本大小a
GD::Graph
,为元素使用较大的字体。请参阅带轴图表的方法。使用
GD::Graph
,您确实不必自己修改这些值。只需提供字符串'.2f%%'
作为y_number_format
的参数。Output:
If you want to extract values in a specific order, use a hash slice:
or, if you just want keys and values to line up in the
plot
call:Note: To increase the text size on a
GD::Graph
, use a larger font for the element. See Methods for charts with axes.Using
GD::Graph
, you really should not have to modify the values yourself. Just provide the string'.2f%%'
as the argument toy_number_format
.http://search.cpan.org/dist/Math-Round/Round.pm
Math::Round 也能创造奇迹。您可以向其传递标量或列表。
http://search.cpan.org/dist/Math-Round/Round.pm
Math::Round also works wonders. You can pass it a scalar or a list.