Php getting ten's value - php

If I have values
2014 I need to get the 14,
2022 I need to get the 22,
2122 I need to get 122,
I could do:
$value = 2000 - $value;
But what if it's
1014? where I need to get the 14?
Is there any way to get the ten's value of any number? or 100's value? Thanks

Yes there is. Use modulus:
<?php
echo 2014%1000;
echo 2022%1000;
echo 2122%1000;
echo 1014%1000;
?>

Yes, there is:
$result = $value % 1000;

If they aren't necessarily numbers and you just want the two left values, you could also use:
substr()
$result = substr($value, -2); // returns last two characters

Yes, If you use % , you can get the reminder value , if your value = 1234 , then 1234 % 1000 will return 234 , similarly it goes on.
$value = 1022 ; // your value
$tens value = $value % 1000;
echo $tens value ;

Related

How do I get the first and the last digit of a number in PHP?

How can I get the first and the last digit of a number? For example 2468, I want to get the number 28. I am able to get the ones in the middle (46) but I can't do the same for the first and last digit.
For the digits in the middle I can do it
$substrmid = substr ($sum,1,-1); //my $sum is 2468
echo $substrmid;
Thank you in advance.
You can get first and last character from string as below:-
$sum = (string)2468; // type casting int to string
echo $sum[0]; // 2
echo $sum[strlen($sum)-1]; // 8
OR
$arr = str_split(2468); // convert string to an array
echo reset($arr); // 2
echo end($arr); // 8
Best way is to use substr described by Mark Baker in his comment,
$sum = 2468; // No need of type casting
echo substr($sum, 0, 1); // 2
echo substr($sum, -1); // 8
You can use substr like this:
<?php
$a = 2468;
echo substr($a, 0, 1).substr($a,-1);
You can also use something like this (without casting).
$num = 2468;
$lastDigit = abs($num % 10); // 8
However, this solution doesn't work for decimal numbers, but if you know that you'll be working with nothing else than integers, it'll work.
The abs bit is there to cover the case of negative integers.
$num = (string)123;
$first = reset($num);
$last = end($num);

Splitting strings through php to query mysql

I need to split a string in two so I can do a SELECT query in my DB. For example, I have the number '0801'. I need to split that number into '08' and '01'. How do I manage to do that?
This is easy to do using the substr() function, which lets you pull out parts of a string. Given your comment that you always have a four-digit number and want it split into two two-digit numbers, you want this:
$input = "0801";
$left = substr($input, 0, 2);
$right = substr($input, 2);
echo "Left is $left and right is $right\n";
According to your comment
The first 2 numbers. It's allways a 4 digit number and I allways need
to split it in 2 two digit numbers
You can simply use
$value = '0801';
$split = str_split($value, 2);
var_dump($split[0]);
var_dump($split[1]);
Just keep in mind $value variable should always be of a string type, not int.
you can use str_split and list
$str = '0801';
list($first,$second) = str_split($str,2);
echo $first;
// 08
echo $second;
// 01
No one gave a MySQL answer yet...
If you're selecting that number..
SELECT
SUBSTR(colname, 0, 2) as firstpart,
SUBSTR(colname, 2, 2) as secondpart
FROM table

How do I echo a variable thats a double, but only show the decimal places if they're needed?

I have some double fields in my database and when echoing the fields out in my php I get .00 at the end of the values.
How do I get the .00 not to display, but display if there is a value?
You can use str_replace to remove the ".00" from the values.
$value = 10.00;
echo str_replace('.00', '', $value); // 10
$value = 10.52;
echo str_replace('.00', '', $value); // 10.52
echo (int)$double;
will simply strip off the decimal places. if you merely want to hide 'zero' decimals (10.00 -> 10), but leave non-zero decimals (10.1 -> 10.1), then you'd need to do some processing:
echo preg_replace('/\.0+$/', '', $double);
which would handle any number of zeroes after the decimal place, but leave non-zeroes in place.
if (fmod($number, 1) == 0)
{
$number = intval($number);
}
else
{
$number = round($number, 2);
}
Or just use round() [# ideone.com]:
var_dump(round($number = 5.00, 2)); // 5
var_dump(round($number = 5.01, 2)); // 5.01
For an arbitrary number of 0s at the end of the number:
$number = rtrim($number,".0");
Examples:
Input : 1.00
Result: 1
Input : 1.25
Result: 1.25
Input : 1.40
Result: 1.4
Input : 1.234910120000
Result: 1.23491012
select number,if(number % 1 = 0,cast(number as unsigned),number)
from table

php nearest ten-th value

I am getting a max value through value. It can be any digit (whole number). I want it to take to next tenth-value, using PHP
Example:
If value is 66, then I need value 70
If value is 6, then I need value 10
If value is 98, then I need value 100
This is an arithmetic problem:
y = ceil(x / 10) * 10
If you’re looking just for the nearest decade, use round instead.
$num = 66;
$val = ceil($num / 10) * 10;
echo $val;
Thanks.
or
echo round(66,-1);
up to 30 characters

convert 01, 02, 03, 04 .. 09 to 1,2,3,4 ... 9

Hay how can I convert
01 to 1
02 to 2
all the way to 9?
Thanks
I assume the input is a string?
$str = "01";
$anInt = intval($str);
You may think that the leading 0 would mean this is interpreted as octal, as in many other languages/APIs. However the second argument to intval is a base. The default value for this is 10. This means 09->9. See the first comment at the intval page, which states that the base deduction you might expect only happens if you pass 0 in as the base.
$x="01";
$x=+$x;
$x="02";
$x=+$x;
...
or
$x=+"01";
should work for both int, and string
Do (int)$str; instead. It's up to 4x faster than intval().
$str = "05";
$last = $str[1];
$i = substr($input, 1, 1);
If you want to use the generic regular expression solution: 's/[0]([0-9])/\1/' (add in anchors as appropriate)
$str='05';
if(strpos($str,'0')==0 && strpos($str,'0') != false ){
$new = intval(substr($str,1));
}
you can use the predefined php function .
intval()
For Ex:
$convert = intval(01);
echo $convert ;
It will print 1(one);

Categories