I've tried the following functions
str_repeat("*", strlen($value['number'])-2) . substr($value['number'], -2)
and str_pad(substr($value['number'], -2), strlen($value['number']), '*', STR_PAD_LEFT)
However they both yield the same result: ******43
What I am tying to achieve is this: 70****43, but dynamically regardless of number length.
Any suggestions how it can be done?
See below:
$num = '70564843';
echo substr( $num, 0, 2 ) // Get the first two digits
.str_repeat( '*', ( strlen( $num ) - 4 ) ) // Apply enough asterisks to cover the middle numbers
.substr( $num, -2 ); // Get the last two digits
Output:
70****43
It's kind of dynamic:
$phoneNumber = 123456789; //your phone number
$showFirstDigits = 2; //how many digits to show in the beggining of the phone number
$showLastDigits = 2; // how many digits to show in the end of the phone number
echo(
substr_replace($phoneNumber,str_repeat('*',strlen($phoneNumber) - $showFirstDigits - $showLastDigits),
$showFirstDigits,
strlen($phoneNumber) - $showFirstDigits - $showLastDigits));
Related
I want to display a large number with a leading zero and a dot after.
The balance i want to display starts with 0.000000000000000000 ( 18 zeros after the dot ). This should be able to go up to 99.00000000000000000.( 17 zeros after the dot ).
I did a lot of trial and error but i just can't seem to get the dot in there. As far as for the zeros i got it working. What i have now is:
$leadingBalance = sprintf("%019d", $balance);
echo $leadingBalance;
This will display the correct balance but i need to place the dot in there. It means that if my balance has 17 or 18 numbers it should place the dot as 0.0000... If the balance has 19 numbers it should place the dot as 00.0000...
Whatever i try, how much i look up i can't figure it out.
For eg:
$n1 = 0;
$n2 = 99;
echo number_format($n1,18)."<br>";
echo number_format($n2,18)."<br>";
See the documentation for number_format: http://php.net/number_format
The functions parameters are:
string number_format ( float $number , int $decimals = 0 , string $dec_point = '.' , string $thousands_sep = ',' )
So use:
number_format(1000000000000000, 2, '.', '');
Which means that you don't use any (= empty string) thousands separator, only a decimal point.
or if you just want padding of 19 zero after decimal point
just use
sprintf("%0.19f",$number);
or else
if u want a number always 20 digit without caring about whole no and decimal value than use str_pad()
eg:
$no = sprintf("%0.2f",100); //100.00
this will convert your no to decimal point with 2 digit after decimal now just pad some digit if require to make it 20 digit long
echo str_pad($no,20,"0"); //100.00(15 zero after this)
this will check no of digit available and pad 0 to make it 20 digit
for more ref:https://www.w3schools.com/php/func_string_number_format.asp
You are using %019d when you actually wants a float number, try this:
<?php
$format = '%0.19f';
$args = 9;
$result = sprintf ($format, $args);
//$result will be equal to 9.0000000000000000000
?>
<?php
$format = '%0.19f';
$args = 99;
$result = sprintf ($format, $args);
//$result will be equal to 99.0000000000000000000
?>
Edit:
Since it looks like you want your whole number to be equal to 20 digits only, you may try to do some math if number_format doesn't do your job. You can try something like:
<?php
$number = 999;
$number_length = strlen($number);
$format_len = 20 - $number_length;
$format = '%0.'. $format_len .'f';
$result = sprintf($format, $number);
?>
I have variables of bitcoin values all rounded to 8 decimal places. eg
1.00645600
I need a way in jQuery or php to get the whole number [1], The decimal values [006456], and trailing zeros [00]. I have already tried php substr but it messed up with the results since im dealing with variables.
Simple and general solution in PHP without involving regular expressions (that is an option also):
$number = '1.00645600';
$flooredNumber = floor($number); // 1
$decimalPart = (string) (floatval($number) - $flooredNumber); // 0.006456
$decimals = str_replace('0.', '', $decimalPart); // 006456
$trailingZeros = str_replace(rtrim($number, '0'), '', $number); // 00
substr
Returns the portion of string specified by the start and length parameters.
http://php.net/manual/en/function.substr.php
If the numbers in your string are always in the same position you can use substr() to get the desired values:
$str = '1.00645600';
echo substr($str, 0, 1)."\r\n";
echo substr($str, 2, 2)."\r\n";
echo substr($str, 2, 6)."\r\n";
Output:
1
00
006456
Perhaps, this way?
<?php
$i = '1.00645600';
echo rtrim(rtrim($i, '0'), '.');
?>
I want to have a PHP number formatted with a minimum of 2 decimal places and a maximum of 8 decimal places. How can you do that properly.
Update: I'm sorry, my question is say I have number "4". I wish for it to display as "4.00" and if I have "2.000000001" then it displays as "2.00" or if I have "3.2102" it will display as such. There is a NSNumber formatter on iPhone, what is the equivalent in PHP.
This formats the $n number for 8 decimals, then removes the trailing zero, max 6 times.
$s = number_format($n, 8);
for($i=0; $i<8-2; $i++) {
if (substr($s, -1) == '0')
$s = substr($s, 0, -1);
}
print "Number = $s";
Use sprintf() to format a number to a certain number of decimal places:
$decimal_places = 4;
$format = "%.${decimal_places}f";
$formatted = sprintf($format,$number);
I don't understand why you would want to display numbers to an inconsistent degree of accuracy. I don't understand what pattern you're trying to describe in your comment, either.
But let us suppose that you want the following behaviour: you want to express the number to 8 decimal places, and if there are more than 2 trailing zeroes in the result, you want to remove the excess zeroes. This is not much more difficult to code than it is to express in English. In pseudocode:
$mystring = string representation of number rounded to 8 decimal places;
while (last character of $mystring is a 0) {
chop off last character of $mystring;
}
Check the number format function:
<?php
$num = 43.43343;
$formatted = number_format(round((float) $num, 2), 2);
?>
http://php.net/manual/en/function.number-format.php
Using preg_match just get the zero ending with and then rtim it
<?php
$nn = number_format(10.10100011411100000,13);
preg_match('/[0]+$/',$nn,$number);
if(count($number)>0){
echo rtrim($nn,$number[0]);
}
Hope it will help you.
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
It will actually be a decimal but that is not the main point. I will have a set of numbers like:
8976
8765
3454
3453
10198
What I am wanting to do is add a decimal 2 places from the right. So the first would be 89.76 and so forth.
Can't you just multiply each by 0.01?
$formatted = number_format($unformatted_number / 100, 2, '.', '');
2 - decimal places
'.' - decimal separator
'' - thousands separator
docs for the function are here.
try this
$number = 8976;
$number = (float)$number/100;
results:
89.76
You may have to do some checking to see how many digits the number is, i.e 89768 would be devided by 1000 and so on.
Comments are available,
//the string you need to split
$string = "123456";
// read from right 2 character
$rightNums = substr($string, -2, 2);
// maximum 100 character to the left defined now
$otherNums = substr($string, -4, 100);
// pront them just with . between
echo $otherNums.".".$rightNums; ?>
hope it help much.
Try with this
$tmpString = substr("8976", 0, -2);
$finalString = str_replace($tmpString, "." . $tmpString, "8976");
echo $finalString;