Author

Topic: number_format() is rounding and I don't want it to... (Read 1285 times)

legendary
Activity: 1064
Merit: 1001
Exactly like that n ya, ill hook you up with 0.00000001 ;-p

Thx !
1 Satoshi? Not sure if thrilled.
Oh wait, that was backwards 10000000.0

Better ;-P

But seriously, thx !
hero member
Activity: 576
Merit: 514
Exactly like that n ya, ill hook you up with 0.00000001 ;-p

Thx !
1 Satoshi? Not sure if thrilled.
legendary
Activity: 1064
Merit: 1001
That works like a charm but with one small flaw.

I added 1234 to he value and get this.

1234.00686725

No comma, so unless your code can be modified to add he comma at the appropriate spots even say at 1000000 then it looks like I'll have to stick with the multiple lines I use. Really liked your sugestion too.
Like that?
Code:
$formatted = preg_replace('/^([\d,]+\.\d\d)(\d+)/', "$1$2", number_format($unmodified_blockchain_balance, 8));

PS: If you ever reach those 1000000, give me a few Wink
Exactly like that n ya, ill hook you up with 0.00000001 ;-p

Thx !
hero member
Activity: 576
Merit: 514
That works like a charm but with one small flaw.

I added 1234 to he value and get this.

1234.00686725

No comma, so unless your code can be modified to add he comma at the appropriate spots even say at 1000000 then it looks like I'll have to stick with the multiple lines I use. Really liked your sugestion too.
Like that?
Code:
$formatted = preg_replace('/^([\d,]+\.\d\d)(\d+)/', "$1$2", number_format($unmodified_blockchain_balance, 8));

PS: If you ever reach those 1000000, give me a few Wink
legendary
Activity: 1064
Merit: 1001
You can do that in a oneliner:
Code:
$formatted = preg_replace('/^(\d+\.\d\d)(\d+)/', "$1$2", $unmodified_blockchain_balance);

That works like a charm but with one small flaw.

I added 1234 to he value and get this.

1234.00686725

No comma, so unless your code can be modified to add he comma at the appropriate spots even say at 1000000 then it looks like I'll have to stick with the multiple lines I use. Really liked your sugestion too.

----------------------------------------------------------------------------------------------------------------------------------------------

Want a sig like mine for your BTC Guild stats, then check out this post !
hero member
Activity: 576
Merit: 514
You can do that in a oneliner:
Code:
$formatted = preg_replace('/^(\d+\.\d\d)(\d+)/', "$1$2", $unmodified_blockchain_balance);
legendary
Activity: 1064
Merit: 1001
If you only want a formatting like 0.00 without rounding, that code is about an order of magnitude slower.
I don't only want the x.xx, I use the last six digits for smaller txt.



The way everything is set, I get my smaller txt and get to numberformat for the commas without rounding the decimal.
hero member
Activity: 576
Merit: 514
If you only want a formatting like 0.00 without rounding, that code is about an order of magnitude slower.
legendary
Activity: 1064
Merit: 1001
I forgot all abou this post and sor of went a differen route with the same results.

Code:
### Blockchain Balance ###
$unmodified_blockchain_balance = ( $blockchain_final_balance / 100000000 );
$blockchain_wallet_bal = substr( sprintf( '%.8f', $unmodified_blockchain_balance ), 0, -9 );
### Blockchain Str Length ###
$blockchain_str = $blockchain_wallet_bal;
$blockchain_str_length = strlen($blockchain_str);
### Blockchain Balance Visuals ###
$formatted_blockchain_wallet_bal1 = number_format( $blockchain_wallet_bal ); // Everything beofore the decimal.
$formatted_blockchain_wallet_bal2 = substr( sprintf( '%.8f', $unmodified_blockchain_balance ), $blockchain_str_length, -6 ); // The decimal & two trailing digits.
$formatted_small_blockchain_wallet_bal = substr( sprintf( '%.8f', $unmodified_blockchain_balance ), -6 ); // Just the last 6 digits.

I know it's a little over complaicated or w/e but it works ;-)
hero member
Activity: 576
Merit: 514
Code:
$formatted_blockchain_wallet_bal = sprintf('%02d', $formatted_blockchain_wallet_bal);
legendary
Activity: 1064
Merit: 1001
A simple way:
Code:
$formatted_blockchain_wallet_bal = intval($unmodified_blockchain_balance * 100) / 100;

Or if you want to make it look odd:
Code:
$formatted_blockchain_wallet_bal = substr($unmodified_blockchain_balance, 0, strpos($unmodified_blockchain_balance, '.') + 3);
Thx for that but eve though it's at 0 now it doesn' display as x.xx like with number format.

Like right now my final nalance is 0.01686725, well the code would ideally display 0.00 and then when I get another 0.004 the ode would / should disaply 0.01.
hero member
Activity: 576
Merit: 514
A simple way:
Code:
$formatted_blockchain_wallet_bal = intval($unmodified_blockchain_balance * 100) / 100;

Or if you want to make it look odd:
Code:
$formatted_blockchain_wallet_bal = substr($unmodified_blockchain_balance, 0, strpos($unmodified_blockchain_balance, '.') + 3);
legendary
Activity: 1064
Merit: 1001
Trying to finish up some work to display BTC info on my site but this damn rounding up that number_format() does is driving me nuts. Anyone know what I need to do to sop the rounding up but still get he x.xx result ?

   $formatted_blockchain_wallet_bal = number_format( $unmodified_blockchain_balance, 2 );
Jump to: