Jump to content

Notation of K and M like RS in PHP


shelledroot

Recommended Posts

For the project I'm working on I am trying to display the GP on page of a mule but I can't get it to correctly show the notation. :x

$holdingAmount = $Mulesrow['holdingamount'];
	if($holdingAmount >= 1000){
		$holdingLength = strlen($holdingAmount);
		$strArg = 1;
		for($i = 0; $i < $holdingLength; $i++){
			$strArg = $strArg * 10;
		}
		if ($holdingAmount >= 1000000) {
				$holdingAmount = floor(($holdingAmount / 1000000) * $strArg ) / $strArg . "M";
		} else if ($holdingAmount >= 1000) {
				$holdingAmount = floor(($holdingAmount / 1000) * 100) / 100 . "K";
		}
	}

I need it to show the correct amount depending on the annotation. 

So if I for example have 21119999 it should show 21.11M instead of 21.12M.
This should be done so depending on the length.

Edited by shelledroot
Link to comment
Share on other sites

There are three different rounding functions, they are often found in all languages:

floor (round down)

round (round up or down)

ceil (round up)

Use floor instead of round.

You need some extra logic if you want to perform the operation to n decimal places:

floor(value * (10 ^ precision)) / (10 ^ precision)

So in this case, as you want it to 2dp it would be:

floor(value * 100) / 100

$holdingAmount = 21119054;

if ($holdingAmount >= 1000000) {
    echo (floor(($holdingAmount / 1000000) * 100) / 100) . "m";	
} else if ($holdingAmount >= 1000) {
    echo (floor(($holdingAmount / 1000) * 100) / 100) . "k";	
} else {
    echo $holdingAmount;	
}

 

I would also strongly recommend that you improve your "Googling" ability.

All you had to do was type "php round down to 2 decimal places".

Edited by Explv
Link to comment
Share on other sites

28 minutes ago, Explv said:

There are three different rounding functions, they are often found in all languages:

floor (round down)

round (round up or down)

ceil (round up)

Use floor instead of round.

You need some extra logic if you want to perform the operation to n decimal places:

floor(value * (10 ^ precision)) / (10 ^ precision)

So in this case, as you want it to 2dp it would be:

floor(value * 100) / 100


$holdingAmount = 21119054;

if ($holdingAmount >= 1000000) {
    echo (floor(($holdingAmount / 1000000) * 100) / 100) . "m";	
} else if ($holdingAmount >= 1000) {
    echo (floor(($holdingAmount / 1000) * 100) / 100) . "k";	
} else {
    echo $holdingAmount;	
}

 

I would also strongly recommend that you improve your "Googling" ability.

All you had to do was type "php round down to 2 decimal places".

Sorry I formed my answer poorly. I'll update it and also rephrase it here:

I want the number notation like RS so 21.11m but no fallover which will be generated due to 21.11999999999.

I need the notation to stay true at all times. Not just depending on the N decimal place but a variable N length. :(

Edited by shelledroot
Link to comment
Share on other sites

3 minutes ago, shelledroot said:

Sorry I formed my answer poorly. I'll update it and also rephrase it here:

I want the number notation like RS so 21.11m but no fallover which will be generated due to 21.11999999999.

I need the notation to stay true at all times. Not just depending on the N decimal place. :(

:???:

The code I just showed you will format it to 21.11m, it will always round down to 2dp...

I have no idea what you are asking now...

Edited by Explv
  • Like 1
Link to comment
Share on other sites

1 hour ago, Explv said:

:???:

The code I just showed you will format it to 21.11m, it will always round down to 2dp...

I have no idea what you are asking now...

	$holdingAmount = $Mulesrow['holdingamount'];
	if($holdingAmount >= 1000){
		$holdingLength = strlen($holdingAmount);
		switch ($holdingLength) {
			case 4:
				$dotPlacementHolding = 1;
				break;
			case 5:
				$dotPlacementHolding = 2;
			break;
			case 6:
				$dotPlacementHolding = 3;
				break;
			case 7:
				$dotPlacementHolding = 1;
				break;
			case 8:
				$dotPlacementHolding = 2;
				break;
			case 9:
				$dotPlacementHolding = 3;
				break;
			case 10:
				$dotPlacementHolding = 4;
				break;
			default:
			break;
		}
		if ($holdingAmount >= 1000000) {
			$holdingAmount = substr_replace($holdingAmount, ".", $dotPlacementHolding, 0);
			$holdingAmount = substr($holdingAmount, 0, -4) . "M";
		} else if ($holdingAmount >= 1000) {
			$holdingAmount = substr_replace($holdingAmount, ".", $dotPlacementHolding, 0);
			$holdingAmount = substr($holdingAmount, 0, -1) . "K";
		}
	}

I sloppy fixed something together.
My initial question was wrong, though you answered that question correctly. Blame that on not enough sleep. I shall sleep and fix it properly in the morning.
What I meant to ask is I want to do proper RS notation How would I do that without rounding the number up or down at a certain N decimal.
This fix works for now until i revisit it but I thought there would be a nicer way to do it.

Edited by shelledroot
Link to comment
Share on other sites

2 minutes ago, shelledroot said:

	$holdingAmount = $Mulesrow['holdingamount'];
	if($holdingAmount >= 1000){
		$holdingLength = strlen($holdingAmount);
		switch ($holdingLength) {
			case 4:
				$dotPlacementHolding = 1;
				break;
			case 5:
				$dotPlacementHolding = 2;
			break;
			case 6:
				$dotPlacementHolding = 3;
				break;
			case 7:
				$dotPlacementHolding = 1;
				break;
			case 8:
				$dotPlacementHolding = 2;
				break;
			case 9:
				$dotPlacementHolding = 3;
				break;
			case 10:
				$dotPlacementHolding = 4;
				break;
			default:
			break;
		}
		if ($holdingAmount >= 1000000) {
			$holdingAmount = substr_replace($holdingAmount, ".", $dotPlacementHolding, 0);
			$holdingAmount = substr($holdingAmount, 0, -4) . "M";
		} else if ($holdingAmount >= 1000) {
			$holdingAmount = substr_replace($holdingAmount, ".", $dotPlacementHolding, 0);
			$holdingAmount = substr($holdingAmount, 0, -1) . "K";
		}
	}

 

 

:???::???::???::???::???:

wtf are you doing

Are you trying to say that you don't want any rounding to occur at all?

Edited by Explv
Link to comment
Share on other sites

4 minutes ago, Explv said:

 

:???::???::???::???::???:

wtf are you doing

Are you trying to say that you don't want any rounding to occur at all?

Yeah I guess so.
Never mind about this. :\

I'm reading an int from a DB table and want to save space on the table row I'm displaying.
But I want it to correctly show. if I just round it at 2 decimals the 3rd decimal flows over still and so on and so forth.

Basically I wanted the RS notation in my table, that code gives me that although it isn't nice looking. :\
I was thinking since there is an pattern there should be a recursive solution to this problem but my mind is in need of sleep.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...