简体   繁体   English

将字符串转换为数字

[英]Converting string to number

It seems to be a very simple question, I use "0888" as my input value example here. 这似乎是一个非常简单的问题,我在这里使用“ 0888”作为输入值示例。 I have researched for a while and tried different ways, such as 我研究了一段时间,尝试了不同的方法,例如

(int) "0888";
intval("0888");
floatval("0888");
settype("0888", "integer");

Unfortunately none of them seems to work. 不幸的是,它们似乎都不起作用。 Reference 1 Reference 2 Reference 3 参考1参考2参考3

$num = $_POST['postcode'];

if((1000 <= $num && $num <= 1999) || (2000 <= $num && $num <= 2599) || (2619 <= $num && $num <= 2898) || (2921 <= $num && $num <= 2999)){
    $state = "NSW";
}
elseif((0200 <= $num && $num <= 0299) || (2600 <= $num && $num <= 2618) || (2900 <= $num && $num <= 2920)){
    $state = "ACT";
}
elseif((3000 <= $num && $num <= 3999) || (8000 <= $num && $num <= 8999)){
    $state = "VIC";
}
elseif((4000 <= $num && $num <= 4999) || (9000 <= $num && $num <= 9999)){
    $state = "QLD";
}
elseif((5000 <= $num && $num <= 5799) || (5800 <= $num && $num <= 5999)){
    $state = "SA";
}
elseif((6000 <= $num && $num <= 6797) || (6800 <= $num && $num <= 6999)){
    $state = "WA";
}
elseif((7000 <= $num && $num <= 7799) || (7800 <= $num && $num <= 7999)){
    $state = "TAS";
}
elseif((0800 <= $num && $num <= 0899) || (0900 <= $num && $num <= 0999)){
    $state = "NT";
}
else {
    $state = "Can not find this postcode record";
}

If I echo $state I expect to see NT but actually I see "Can not find this postcode record" instead. 如果我echo $state我希望看到NT,但实际上我看到的是“找不到此邮政编码记录”。 Can anyone tell me what the problem is? 谁能告诉我问题出在哪里?

0299 is the octal notation of a number, which is very different from the decimal 299. In fact, 9 is invalid in octal and 0299 just has the value 2. Try echo 0299; 0299是数字的八进制表示法 ,与十进制 299截然不同。实际上,八进制的9无效,而0299仅具有值2。请尝试echo 0299; , then echo 0123; ,然后echo 0123; .

If you're treating postcodes as numbers at all, you should strip leading 0s from it and don't use leading 0s in companions: 如果您将邮政编码完全视为数字,则应从中去除前导0,并且不要在随播广告中使用前导0:

$code = ltrim($_POST['postcode'], '0');

... $code <= 299 ...

Use trim then ltrim for zeros, the second parameter allows using any characters you want to trim. 使用trim然后将ltrim设为零,第二个参数允许使用要修剪的任何字符。

<?php
$num = $_POST['postcode'];
$num = ltrim( trim($_POST['postcode'], ' ') , '0'); //trim SPACES or ZERO

If you are sure the number comes in the left you can use ltrim( 如果您确定数字在左侧,则可以使用ltrim(

// Just to be sure, finally reconvert it to (int)
$num = (int) $num;

intval will by default parse your strings base 10 so $num = intval("0888"); 默认情况下, intval将以10 $num = intval("0888");基础解析您的字符串,因此$num = intval("0888"); will correctly parse the string to 888. No need to trim. 会将字符串正确解析为888。无需修剪。

The integer literals in your if statements, however, get interpreted as octal numbers. 但是,if语句中的整数文字将被解释为八进制数字。 Remove the leading zeros there: 删除那里的前导零:

[...] elseif((800 <= $num && $num <= 899)  [...]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM