The solution is simpler than you think.
Instead of using %g use %f and this will lead to the behavior you are looking for. %f always outputs your floating decimal to "fixed decimal notation".
What does the documentation say about %g vs %f ?
As you can see in the table below, %g will lead to the same thing as %f or %e (if necessary).
Ff, you want to force use fixed decimal notation, use the appropriate format identifier, which in this case is %f .
sprintf - perldoc.perl.org
%% a percent sign %ca character with the given number %sa string %da signed integer, in decimal %u an unsigned integer, in decimal %o an unsigned integer, in octal %x an unsigned integer, in hexadecimal %ea floating-point number, in scientific notation %fa floating-point number, in fixed decimal notation %ga floating-point number, in %e or %f notation
What about TIMTOWTDI; don't we write perl?
Yes, as always, there are several ways to do this.
If you want to trim null zero decimal points from a string , you can use regex like below.
$number = "123000.321000"; $number =~ s/(\.\d+?)0+$/$1/; $number
Remember that floating point values in perl do not have trailing commas, unless you are dealing with a string. With that said; a string is not a number, although it can be explicitly and implicitly converted to one.
source share