Help with displaying the correct number format

#1

The web script I’m using displays the User ‘balance’ amount like so: 00.00, but because the balance amount is in units, I’d like it to display as 00.0, for example 8.5, instead of 8.50.

After looking around, I see the php file showing these lines pertaining to ‘balance’:

$balance = (float)str_replace(',', '', $user->balance);

'balance' => number_format($videouserwallet, 1, '.', ''),

and the html (where the amount is actually displayed):

{{LINK settings/balance/<?php echo $pt->settings->username; ?>}}" data-load="?link1=settings&page=balance&user=<?php echo $pt->settings->username; ?>">
 {{LANG balance_}} : {{ME balance}}

and the db table column, named: ‘balance’ shows the structure as Type: String varchar(100)

Any help with where/how I might make a change to accomplish the corrected decimal modification, is appreciated.

#2

If you declared(strict_types=1); error_reporting(-1): ini_set(‘display_errors’, ‘true’); at the top of the page script I am sure errors and warnings would highlight exactly where the problem occurs.

#3

Thanks for your reply.

Sorry, if my posting was not clear, regarding “where the problem occurs”, there isn’t one. It works successfully. I’m looking for help as to how/where I can make the modification that I’ve described.

Any help with that is appreciated

#4

The second line does not reference the first. The second line would generate output in the format you wanted.

#5

Thanks for your reply “The second line would generate output in the format you want”.
Yet, it doesn’t.

This code comes after that line (maybe ‘balance’ needs to be re-formated?):

if($wallet >= $amount){

$wallet = (string)($wallet - $amount);
$db->where('id', $user_id);
$update_wallet = $db->update(T_USERS, [
'wallet' => $wallet
]);

}else if ($wallet + $balance >= $amount) {
$balance = (string)($balance - ($amount - $wallet));
$wallet = (string)($wallet - $wallet);
$db->where('id', $user_id);
$update_user_balance = $db->update(T_USERS, [
'balance' => $balance
]);
$update_wallet = $db->update(T_USERS, [
'wallet' => $wallet
]);
} else {
echo json_encode([
'status' => 400,
'error_num' => 1,
'error' => 'Not enough money'
]);
exit();
}

any additional guidance is appreciated

#6

Your original post produces the following error because a string cannot be allocated a value. Only values may be allocated variables:

Tested here:

<?php 
declare(strict_types=1);
error_reporting(-1);
ini_set('display_errors', 'true');

'balance' => number_format($videouserwallet, 1, '.', '');

Output:

Parse error : syntax error, unexpected ‘=>’ (T_DOUBLE_ARROW) in /var/www/example.com/public_html/index.php on line 6

#7

Try showing the following values:

<?php
declare(strict_types=1);
error_reporting(-1);
ini_set('display_errors' 'true');

// ...
// ...

echo '<br> line: ' .__line__
     . ' <br> $wallet ==> ' .$wallet 
     . ' <br>$amount ==> '. $amount
     ; 
die('<br><br>Move these two lines down until problem solved');


#8

@chrisjchrisj hasn’t mentioned that this line:

'balance' => number_format($videouserwallet, 1, '.', ''),

is part of an array declaration that makes it valid syntax. (ETA - his later post shows a bit more of it as part of a function call, not quite where I was thinking of).

This doesn’t seem an appropriate column type for a numeric balance.

I recall you asked before about changing the way a number was displayed, using this same template system that sits on top of the standard html. I recall you mentioned you’d sorted it, but I don’t think you said how.

To clarify, are you trying to change the way it’s stored in the database (as part of your call to $db->update() or are you trying to change the way it’s displayed? The latter would be better.

3 Likes
#9

Thanks for the replies.
“are you trying to change the way it’s displayed?”
yes

#10

the db table column, named: ‘balance’ shows the structure as Type: String varchar(100)

Any help with how I might make a change to accomplish the corrected decimal modification, which is 0.0 instead of 0.00, is appreciated.

#11

Agreed, it makes no sense to store numeric values as varchar.
Being a decimal value decimal may be a good choice of data type for that column. When you define the column format you also define the number of decimal places, along with the total number of digits (including decimals). So 0000.0 would be decimal (5, 1) that’s five digits and one decimal place, adjust the numbers to what you need, considering the maximum expected values, which in that example would be: 9999.9
Though one decimal for currency still seems odd to me as most currencies are divided into cents (or variations of).

1 Like
#12

Thanks for your reply/info.
I have followed your instructions and the ‘balance’ column structure now shows:
Type: DECIMAL
Length/Values: 5,1
Default: As Defined 0.0
yet, the html displayed number still shows as 0.00

Maybe you could comment on adding something like this:

'update_user_balance' => number_format($balance, 1, '.', '');

to this last part of the buy-video.php file code:

		// if all the video records were inserted and the current user's wallet was updated, commit the changes
		if (($inserted_records == $count_video) && ($update_wallet OR $update_user_balance) ) {
			$db->commit();

			echo json_encode([
				'status' => 200
			]);
			exit();
		} else {
			$db->rollback();

			echo json_encode([
				'status' => 400,
				'_data' => [
					'inserted_records' => $inserted_records,
					 'count_video' => $count_video,
					  'update_wallet' => $update_wallet,
					   'update_user_balance' => $update_user_balance
				],
				'error' => 'Buy process error'
			]);
			exit();
		}

	} else {

		echo json_encode([
			'status' => 400,
			'error_num' => 1,
			'error' => 'Not enough money'
		]);
		exit();

if you think that might display as wanted, is my code attempt correct? If so, where should I place it. If not, any additional guidance is appreciated.

#13

Did you try changing 1 to 2?

#14

One of these things is not like the other~

#15

Thanks for the replies.

So, is this valid code?

‘update_user_balance’ => number_format($balance, 1, ‘.’, ‘’);

and if I change 1 to 2, what will that do?

I look forward to hearing back. Much thanks again.

#16

do you get syntax errors?

why don’t you just RTFM?

https://www.php.net/manual/en/function.number-format.php

why don’t you try it yourself?

1 Like
#17

This is the section I was talking about:

In there, you ask about how to get it to display earnings with a decimal place, then a couple of posts later, you got it working. So perhaps you could have a look back at what you did there to make the display as you wanted.

What’s the name of the template system you use? I’m sure someone else knew what it was in another post but I can’t remember. I wonder whether whatever part of it translates

<p>{{ME balance}}</p>

into displaying that actual variable you pass in also handles formatting it, and that would explain why, whatever format you pass into it, it still comes out the same.

1 Like