Why Does 'Intval(19.9 * 100)' Equal '1989'

Why does `intval(19.9 * 100)` equal `1989`?

This is a precision issue inherent to floating point numbers in PHP, and lots of other languages. This bug report discusses it a bit, in the context of casting as an int:

http://bugs.php.net/bug.php?id=33731

Try round($val * 100) instead.

PHP: intval() equivalent for numbers = 2147483647

Try this function, it will properly remove any decimal as intval does and remove any non-numeric characters.

<?php
function bigintval($value) {
$value = trim($value);
if (ctype_digit($value)) {
return $value;
}
$value = preg_replace("/[^0-9](.*)$/", '', $value);
if (ctype_digit($value)) {
return $value;
}
return 0;
}

// SOME TESTING
echo '"3147483647.37" : '.bigintval("3147483647.37")."<br />";
echo '"3498773982793749879873429874.30872974" : '.bigintval("3498773982793749879873429874.30872974")."<br />";
echo '"hi mom!" : '.bigintval("hi mom!")."<br />";
echo '"+0123.45e6" : '.bigintval("+0123.45e6")."<br />";
?>

Here is the produced output:

"3147483647.37" : 3147483647
"3498773982793749879873429874.30872974" : 3498773982793749879873429874
"hi mom!" : 0
"+0123.45e6" : 0

Hope that helps!

Is there any particular difference between intval and casting to int - `(int) X`?

intval() can be passed a base from which to convert. (int) cannot.

int intval( mixed $var  [, int $base = 10  ] )

How do I convert a string to a number in PHP?

You don't typically need to do this, since PHP will coerce the type for you in most circumstances. For situations where you do want to explicitly convert the type, cast it:

$num = "3.14";
$int = (int)$num;
$float = (float)$num;

PHP Mysqli Equal operator not working

"NOW() - INTERVAL 11 DAY" will return a date in the datetime format:
2016-06-20 16:22:26

If "joindate" is not in that format and instead in a date format it will not match anything. You should then use:
"CURDATE() - INTERVAL 11 DAY"

Cheers

Why does 1 equal +1?

When using == instead of === a loose comparison is used.

This often leads to types using truthyness when comparing incomparable types

have a look a the manual here to see the table of comparison
http://php.net/manual/en/types.comparisons.php

even though "+1" is not mentioned, it would be handled similarly to a '-1'

If you don't want this to happen, you do need to use ===

comparing doubles with adaptive approximately equal

float and double do not have precision in the way you are thinking. Programs fake precision by truncating trailing zeroes...but you can't make use of this trick for your purposes, since rounding errors will prevent such a trick from being reliable.

decimal does keep track of how many digits to place to the right of the decimal, but this is still worthless for implementing the algorithm you propose, as any an operation which introduces a representational error (e.g., dividing by 3) will tend to max out the the number of digits to the right of the decimal.

If you want to actually have fuzzy equality based on the known precision of your data, one way to do it would be to create your own number class. Something like this:

public class DoublePrecise
{
public readonly double Error;
public readonly double Value;
public DoublePrecise(double error, double value) {
Error = error;
Value = value;
}
public static DoublePrecise operator -(DoublePrecise d1, DoublePrecise d2) {
return new DoublePrecise(d1.Value - d2.Value, d1.Error + d2.Error);
}
//More stuff.
}

Basically, this is letting your represent numbers like 10.0±0.1. In that situation, you would treat two numbers as being approximately equal if their ranges overlap (though in reality, this would make your equality operation mean, "could be equal" and your inequality operation mean, "definitely not equal."

See also, Interval Arithmetic

Lua string to int

Use the tonumber function. As in a = tonumber("10").

Unexpected behaviour of Intval

It is happening because of the conversion & type juggling of comparison operators.

intval('anystring') will be 0.

And when a string is getting compared it is also converted into numeric value. So when the string is converted it will also be 0.

If you compare a number with a string or the comparison involves numerical strings, then each string is converted to a number and the comparison performed numerically. These rules also apply to the switch statement. The type conversion does not take place when the comparison is === or !== as this involves comparing the type as well as the value.

So in this case 'google1' == intval('google') will be 0 == 0 and that is true. For this type of comparison always use identical(===) comparison.

comparison

getting error value '0' is not a valid value

"Value '0' is not a valid value for interval. interval must be greater than 0."

The obvious reason is because of ClickIntervalNum1 = Convert.ToInt32(ClickIntervalStr1);. The value of ClickIntervalStr1 is evident of 0...

Microsoft has this to say about the Interval property:

The time, in milliseconds, between Elapsed events. The value must be greater than zero, and less than or equal to MaxValue. The default is 100 milliseconds.

To fix this, just make sure it's greater than 0... (simple example):

 timerClickProcessStopAfterXTimes.Interval = ClickIntervalNum1 > 0 ? ClickIntervalNum1 : 100;

References:

Timer.Interval Property System.Timers



Related Topics



Leave a reply



Submit