Bitcoin Forum

Bitcoin => Bitcoin Technical Support => Topic started by: MoreBloodWine on January 29, 2014, 04:59:14 PM



Title: number_format() is rounding and I don't want it to...
Post by: MoreBloodWine on January 29, 2014, 04:59:14 PM
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 );


Title: Re: number_format() is rounding and I don't want it to...
Post by: Bitsky on January 29, 2014, 09:51:49 PM
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);


Title: Re: number_format() is rounding and I don't want it to...
Post by: MoreBloodWine on January 29, 2014, 11:33:43 PM
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.


Title: Re: number_format() is rounding and I don't want it to...
Post by: Bitsky on January 31, 2014, 09:36:29 AM
Code:
$formatted_blockchain_wallet_bal = sprintf('%02d', $formatted_blockchain_wallet_bal);


Title: Re: number_format() is rounding and I don't want it to...
Post by: MoreBloodWine on January 31, 2014, 09:51:02 AM
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 ;-)


Title: Re: number_format() is rounding and I don't want it to...
Post by: Bitsky on January 31, 2014, 10:49:45 AM
If you only want a formatting like 0.00 without rounding, that code is about an order of magnitude slower.


Title: Re: number_format() is rounding and I don't want it to...
Post by: MoreBloodWine on January 31, 2014, 11:40:26 AM
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.

http://www.eojmarket.com/NonForumStuff/images/btc_node_and_btcguild_earnings_info.jpg

The way everything is set, I get my smaller txt and get to numberformat for the commas without rounding the decimal.


Title: Re: number_format() is rounding and I don't want it to...
Post by: Bitsky on January 31, 2014, 02:28:35 PM
You can do that in a oneliner:
Code:
$formatted = preg_replace('/^(\d+\.\d\d)(\d+)/', "$1<small>$2</small>", $unmodified_blockchain_balance);


Title: Re: PB Mining -- 5 year mining contracts!
Post by: MoreBloodWine on January 31, 2014, 03:38:38 PM
You can do that in a oneliner:
Code:
$formatted = preg_replace('/^(\d+\.\d\d)(\d+)/', "$1<small>$2</small>", $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 (https://bitcointalk.org/index.php?topic=438133.msg4814132#msg4814132) post !
http://www.eojmarket.com/BTC-Guild_Stat_Sig/btcg_stat_sig-MoreBloodWine.php


Title: Re: PB Mining -- 5 year mining contracts!
Post by: Bitsky on January 31, 2014, 08:21:12 PM
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<small>$2</small>", number_format($unmodified_blockchain_balance, 8));

PS: If you ever reach those 1000000, give me a few ;)


Title: Re: PB Mining -- 5 year mining contracts!
Post by: MoreBloodWine on January 31, 2014, 10:37:17 PM
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<small>$2</small>", number_format($unmodified_blockchain_balance, 8));

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

Thx !


Title: Re: PB Mining -- 5 year mining contracts!
Post by: Bitsky on January 31, 2014, 11:56:25 PM
Exactly like that n ya, ill hook you up with 0.00000001 ;-p

Thx !
1 Satoshi? Not sure if thrilled.


Title: Re: PB Mining -- 5 year mining contracts!
Post by: MoreBloodWine on February 01, 2014, 12:01:41 AM
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 !