Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Print Number With Implied Decimal Point

by tobyink (Canon)
on Apr 18, 2012 at 14:17 UTC ( [id://965720]=note: print w/replies, xml ) Need Help??


in reply to Print Number With Implied Decimal Point

Regular expressions should work.

Alternatively, try Math::BigFloat.

use 5.010; use Math::BigFloat; my $number = "999999999999999999999900"; my $decimal_places = 2; Math::BigFloat->precision(-$decimal_places); say Math::BigFloat->new($number)/(10**$decimal_places); __END__ 9999999999999999999999.00

substr will be faster though.

perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

Replies are listed 'Best First'.
Re^2: Print Number With Implied Decimal Point
by Eliya (Vicar) on Apr 18, 2012 at 14:51 UTC
    substr will be faster though.

    Yes, by several orders of magnitude...

    use Benchmark qw(cmpthese); use Math::BigFloat; Math::BigFloat->precision(-2); my $number = "999999999999999999999900"; cmpthese -1, { substr => sub { my $num=$number; substr($num,-2,0) = '.'; }, BigFloat => sub { my $num=$number; Math::BigFloat->new($num)/100; +}, }; __END__ Rate BigFloat substr BigFloat 3794/s -- -100% substr 1668189/s 43874% --

    And that doesn't even involve accessing or printing out the constructed BigFloat again.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://965720]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2024-04-19 21:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found