$databtcguild = file_get_contents('http://btcguild.com');
preg_match('~<b>Pool Speed</b></a> (.*?) TH/s~',$databtcguild,$btcguild);
$btcguildhashrategh = ($btcguild[1] * 1000);
echo $btcguildhashrategh;
echo "<br>";
echo $btcguild[1];
For some reason this code is outputting the wrong answer. For example, $btcguild[1] will equal 12,747 and this code will output 12000. I'm completely lost here. Thanks for any help.
The "hash speed" value you are extracting from that site has a value with a comma in it:
12,747
PHP needs to convert this string to a numeric value, and the comma causes the numeric value 12 to be returned (, is interpreted as a decimal)
Make sure you strip all non-numeric characters before multiplying:
//keep only values 0-9 and decimal (period)
$hash_speed = preg_replace("/[^0-9.]/", "", $btcguild[1]);
$btcguildhashrategh = ($hash_speed * 1000); //returns 12747000
Try explicitly type-casting the result: $btcguildhashrategh = ((double)$btcguild[1] * 1000);
Otherwise PHP will convert it into an int.
Related
here is field name "phone number". i want to store data in the database table without leading zero when a user can input phone number like '01323442234' or '1323442234'.
input = '01323442234' or '1323442234'
store = '1323442234' (skip first zero)
$trimmed_phone = ltrim($input, "0");
ltrim will trim all the leading character from a string, no other characters will be removed.
doc: https://www.php.net/manual/en/function.ltrim.php
There are a lot of way to remove 0 from the first part of a string.
If it contains only number, then cast it to integer
$var = (int)$var;
You can use left trim as follow :
$var = ltrim($var, '0');
Just use + inside variables:
echo +$var;
Multiple it by 1 :
$var = "0000000000010";
print $var*1; // prints 10
Note : If your string contains without number, then only use ltrim
I am trying to convey $string to float. The type of $string is string in the format of "0.0111455667". I have the following code in php.I have tried all these methods and I got 0 for all of them. how can I convert the string to float?why I always get 0?
PS: please do not assume my question as duplicate, I have already tried all the methods in the similar questions and none of them worked for me.
$float = (float) $string;
//$float2 = $string + 0.0; //this works as well.
$floatval = floatval($string);
$double = (double) $string;
// TEST
echo $string;
echo $float;
//echo $float2;
echo $floatval;
echo $double;
Your problem is the content of $string.
In PHP, every time you convert a string to a float, be it by casting (float) $string, or floatval($string), you will always get 0 if the first character of the string is not numeric.
From PHP docs: String conversion to numbers
The value is given by the initial portion of the string. If the string starts with valid numeric data, this will be the value used. Otherwise, the value will be 0 (zero)
Double-check the content of $string. Probably you have some spurious characters at its beginning.
I have this PHP code:
<?php
$float = "1,99";
echo "<p>$float<br>";
$float = floatval($float); // Without this line number_format throws a notice "A non well formed numeric value encountered" / (float) $float leads to the same output
$val = number_format($float, 2,'.', ',');
echo "$float</p>";
?>
Why does it return 1? Don't get that.
And: yes, there is a sense in converting 1,99 to 1,99 ;-)
Thanks for advise...
The problem is, that PHP does not recognize the , in 1,99 as a decimal separator. The float type is defined as having the following formal definition:
LNUM [0-9]+
DNUM ([0-9]*[\.]{LNUM}) | ({LNUM}[\.][0-9]*)
EXPONENT_DNUM [+-]?(({LNUM} | {DNUM}) [eE][+-]? {LNUM})
That means it'll only accept . as a decimal separator. That's in fact the same reason why number_format throws a warning on an invalid datatype because it cannot convert 1,99 to a float internally.
The following should work:
$float = "1,99";
echo "<p>$float<br>";
$val = number_format(str_replace(',', '.', $float), 2,'.', ',');
echo "$float</p>";
floatval recognizes the comma (,) as a character and not as a number, so it cuts off everything that comes after it. In this case, that's the 99. Please use a dot (.) instead of a comma (,) and it will probably work.
Example floatval (source: http://php.net/manual/en/function.floatval.php):
<?php
$var = '122.34343The';
$float_value_of_var = floatval($var);
echo $float_value_of_var; // 122.34343
?>
1,99 is not a valid php float. The issue is your comma (,). In PHP you have to use dot (.) as floating point separator.
<?php
$float = "1.99";
echo "<p>$float<br>";
$float = (float)$float;
$val = number_format($float, 2,'.', ',');
echo "$float</p>";
?>
I have script that identifies with preg_match_all some numbers from a given file and in a given format '#(\d\,\d\d\d\d)#' (decimal, with 4 decimals). With them, later, I need to do some math operations to find out the sum, average etc.
With print_r I can see all matches from the array and it is ok (4,3456, 4,9098, etc.). I verify the type of variables and gettype() returned string
Unfortunately I cannot do math operations with them because when I use the variables in a math expression the result is always rounded regardless of what came afer the comma.
For example:
4,3456 + 4,9098 + 4,3456 = 12, or 12,0000 -- if I use number_format.
I used . instead of , in the numbers, I formatted the results with number_format, but have had no success. It seems I am missing something.
Thanks for help!
The error happens even before the number_format call -- PHP considers . as the decimal separator, not ,. you need to str_replace all your array elements:
$values_array = str_replace(",", ".", $values_array)
PHP uses the . character as decimal separator, so you have to replace the , by a . in your matched numbers before converting them to numbers:
$number = floatval(strtr("1,234", ",", "."));
// 1.234
Example:
<?php
$numbers = array("1,234", "5,67");
$numbers = str_replace(",", ".", $numbers);
echo number_format($numbers[0] + $numbers[1], 4, ',', ' ');
Try it here: http://codepad.org/LeeTiKPF
I have this code and i make a cast to remove the symbol €.
$t = "€2000";
$venc = (int)$t;
echo $venc; // actually echo is 0 and i want 2000 (remove symbol)
The output is 0 and not 2000, so, the code is not working as i expect.
What is the reason for (int)$t; not echo 2000 ?
thanks
casting routine does not remove invalid characters, but start from the beginning and stops when first invalid character is reached then convert it to number, in your case Euro sign is invalid and it is the first character thus resulting number is 0.
check http://www.php.net/manual/en/language.types.string.php#language.types.string.conversion
you could try (int)preg_replace('/\D/ui','',$t);
however if you are dealing with currencies you should not forget that they are not integers but floats
(float)preg_replace('/[^0-9\.]/ui','',$t);
This can help you
$t = preg_replace('/[^0-9]/i', '','€2000');
$venc = (int)$t;
echo $venc;
$t = "€2000";
$venc = (int)substr($t,1);
echo $venc;
use substr
echo substr($t,3); // returns "2000"