Bitcoin Forum
May 03, 2024, 05:36:53 AM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: number_format() is rounding and I don't want it to...  (Read 1257 times)
MoreBloodWine (OP)
Legendary
*
Offline Offline

Activity: 1050
Merit: 1001


View Profile
January 29, 2014, 04:59:14 PM
 #1

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 );

To be decided...
1714714613
Hero Member
*
Offline Offline

Posts: 1714714613

View Profile Personal Message (Offline)

Ignore
1714714613
Reply with quote  #2

1714714613
Report to moderator
1714714613
Hero Member
*
Offline Offline

Posts: 1714714613

View Profile Personal Message (Offline)

Ignore
1714714613
Reply with quote  #2

1714714613
Report to moderator
Whoever mines the block which ends up containing your transaction will get its fee.
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction.
1714714613
Hero Member
*
Offline Offline

Posts: 1714714613

View Profile Personal Message (Offline)

Ignore
1714714613
Reply with quote  #2

1714714613
Report to moderator
1714714613
Hero Member
*
Offline Offline

Posts: 1714714613

View Profile Personal Message (Offline)

Ignore
1714714613
Reply with quote  #2

1714714613
Report to moderator
1714714613
Hero Member
*
Offline Offline

Posts: 1714714613

View Profile Personal Message (Offline)

Ignore
1714714613
Reply with quote  #2

1714714613
Report to moderator
Bitsky
Hero Member
*****
Offline Offline

Activity: 576
Merit: 514


View Profile
January 29, 2014, 09:51:49 PM
 #2

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);

Bounty: Earn up to 68.7 BTC
Like my post? Feel free to drop a tip to 1BitskyZbfR4irjyXDaGAM2wYKQknwX36Y
MoreBloodWine (OP)
Legendary
*
Offline Offline

Activity: 1050
Merit: 1001


View Profile
January 29, 2014, 11:33:43 PM
 #3

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.

To be decided...
Bitsky
Hero Member
*****
Offline Offline

Activity: 576
Merit: 514


View Profile
January 31, 2014, 09:36:29 AM
 #4

Code:
$formatted_blockchain_wallet_bal = sprintf('%02d', $formatted_blockchain_wallet_bal);

Bounty: Earn up to 68.7 BTC
Like my post? Feel free to drop a tip to 1BitskyZbfR4irjyXDaGAM2wYKQknwX36Y
MoreBloodWine (OP)
Legendary
*
Offline Offline

Activity: 1050
Merit: 1001


View Profile
January 31, 2014, 09:51:02 AM
 #5

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 ;-)

To be decided...
Bitsky
Hero Member
*****
Offline Offline

Activity: 576
Merit: 514


View Profile
January 31, 2014, 10:49:45 AM
 #6

If you only want a formatting like 0.00 without rounding, that code is about an order of magnitude slower.

Bounty: Earn up to 68.7 BTC
Like my post? Feel free to drop a tip to 1BitskyZbfR4irjyXDaGAM2wYKQknwX36Y
MoreBloodWine (OP)
Legendary
*
Offline Offline

Activity: 1050
Merit: 1001


View Profile
January 31, 2014, 11:40:26 AM
 #7

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.

To be decided...
Bitsky
Hero Member
*****
Offline Offline

Activity: 576
Merit: 514


View Profile
January 31, 2014, 02:28:35 PM
 #8

You can do that in a oneliner:
Code:
$formatted = preg_replace('/^(\d+\.\d\d)(\d+)/', "$1<small>$2</small>", $unmodified_blockchain_balance);

Bounty: Earn up to 68.7 BTC
Like my post? Feel free to drop a tip to 1BitskyZbfR4irjyXDaGAM2wYKQknwX36Y
MoreBloodWine (OP)
Legendary
*
Offline Offline

Activity: 1050
Merit: 1001


View Profile
January 31, 2014, 03:38:38 PM
 #9

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 post !

To be decided...
Bitsky
Hero Member
*****
Offline Offline

Activity: 576
Merit: 514


View Profile
January 31, 2014, 08:21:12 PM
 #10

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 Wink

Bounty: Earn up to 68.7 BTC
Like my post? Feel free to drop a tip to 1BitskyZbfR4irjyXDaGAM2wYKQknwX36Y
MoreBloodWine (OP)
Legendary
*
Offline Offline

Activity: 1050
Merit: 1001


View Profile
January 31, 2014, 10:37:17 PM
 #11

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 Wink
Exactly like that n ya, ill hook you up with 0.00000001 ;-p

Thx !

To be decided...
Bitsky
Hero Member
*****
Offline Offline

Activity: 576
Merit: 514


View Profile
January 31, 2014, 11:56:25 PM
 #12

Exactly like that n ya, ill hook you up with 0.00000001 ;-p

Thx !
1 Satoshi? Not sure if thrilled.

Bounty: Earn up to 68.7 BTC
Like my post? Feel free to drop a tip to 1BitskyZbfR4irjyXDaGAM2wYKQknwX36Y
MoreBloodWine (OP)
Legendary
*
Offline Offline

Activity: 1050
Merit: 1001


View Profile
February 01, 2014, 12:01:41 AM
 #13

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 !

To be decided...
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!