如何更改最后一位数字

时间:2014-07-29 04:58:31

标签: php

将价格更改为始终显示为:29,39,49,59,69,79,89或99 例如:如果价格是2018 = 2029
或者,如果价格是1051,那么加价为:1059

6 个答案:

答案 0 :(得分:3)

看起来很简单。将数字分成两部分(下面的伪代码):

remainder = number % 100           # gives 0 thru 99 inclusive
hundreds = number - remainder      # give x00, where x is any sequence of digits.

然后用以下内容调整余数:

if      remainder <= 29:  remainder = 29
else if remainder <= 39:  remainder = 39
else if remainder <= 49:  remainder = 49
else if remainder <= 59:  remainder = 59
else if remainder <= 69:  remainder = 69
else if remainder <= 79:  remainder = 79
else if remainder <= 89:  remainder = 89
else:                     remainder = 99

然后重新组合他们:

number = hundreds + remainder

如果您愿意,可以将中间位简化为表达式,但是,根据业务逻辑,如果边界变为.29.37,最好不要原样离开,.53(或任何不能立即服从公式的东西)。


举例来说,这里有一些Python代码(最终的伪代码语言)可以满足您的需求:

def adjust (n):
    r = n % 100
    n = n - r

    if   r <= 29:  r = 29
    elif r <= 39:  r = 39
    elif r <= 49:  r = 49
    elif r <= 59:  r = 59
    elif r <= 69:  r = 69
    elif r <= 79:  r = 79
    elif r <= 89:  r = 89
    else:          r = 99

    return n + r


print "%4d -> %4d\n" % (1562, adjust(1562))
for i in range (89,190):
    print "%3d -> %3d" % (i, adjust(i))

输出符合预期:

1562 -> 1569

 89 ->  89
 90 ->  99
 91 ->  99
 92 ->  99
 93 ->  99
 94 ->  99
 95 ->  99
 96 ->  99
 97 ->  99
 98 ->  99
 99 ->  99
100 -> 129
101 -> 129
102 -> 129
103 -> 129
104 -> 129
105 -> 129
106 -> 129
107 -> 129
108 -> 129
109 -> 129
110 -> 129
111 -> 129
112 -> 129
113 -> 129
114 -> 129
115 -> 129
116 -> 129
117 -> 129
118 -> 129
119 -> 129
120 -> 129
121 -> 129
122 -> 129
123 -> 129
124 -> 129
125 -> 129
126 -> 129
127 -> 129
128 -> 129
129 -> 129
130 -> 139
131 -> 139
132 -> 139
133 -> 139
134 -> 139
135 -> 139
136 -> 139
137 -> 139
138 -> 139
139 -> 139
140 -> 149
141 -> 149
142 -> 149
143 -> 149
144 -> 149
145 -> 149
146 -> 149
147 -> 149
148 -> 149
149 -> 149
150 -> 159
151 -> 159
152 -> 159
153 -> 159
154 -> 159
155 -> 159
156 -> 159
157 -> 159
158 -> 159
159 -> 159
160 -> 169
161 -> 169
162 -> 169
163 -> 169
164 -> 169
165 -> 169
166 -> 169
167 -> 169
168 -> 169
169 -> 169
170 -> 179
171 -> 179
172 -> 179
173 -> 179
174 -> 179
175 -> 179
176 -> 179
177 -> 179
178 -> 179
179 -> 179
180 -> 189
181 -> 189
182 -> 189
183 -> 189
184 -> 189
185 -> 189
186 -> 189
187 -> 189
188 -> 189
189 -> 189

答案 1 :(得分:2)

试试这个:

if (substr($price, -2) >= 20) {
    $price = substr($price, 0, -1) . 9;
} 
else {
    $price = substr($price, 0, -2) . 29; 
}

see demo Updated with multiple examples


在评论中以@HankyPanky的建议更新。为简单起见,请使用三元运算符:

$price = substr($price,-2) >= 20 ? substr($price,0,-1) . 9 : substr($price,0,-2) . 29;

答案 2 :(得分:2)

需要使用substr函数比较最后2个值,如上所示。然后检查最近可能值的条件。 你去吧完成。

if (substr($price, -2) <= 29) {
    $price = substr($price, 0, -2) . 29;
} 
else if  (substr($price, -2) <=39) {
    $price = substr($price, 0, -2) . 39; 
}

依旧......

干杯...

答案 3 :(得分:2)

如果是浮动号码,则无效。

首先需要检查值是整数(十进制)还是浮点数。 之后,您需要相应地处理逻辑。

制作如下功能:

function addVal($price){
    $price = 10.50;
    if(is_float($price)){
        $whole = floor($price);      // 1
        $fraction = $n - $whole;
        $final = getVal($whole,$fraction);
    }else{
        $final = getVal($price);
    }
    return $final;
}
function getVal($price,$fraction=0){
    // your logic goes here
    if($fraction!=0){
        // add fractional part to price after applying logic
    }
    return $price;
}

此致

答案 4 :(得分:1)

你可以通过使用PHP的圆函数来实现你的php函数

答案 5 :(得分:0)

它正在全力以赴。我在本地服务器上测试过。

<?php
 $variable=1255;
 $len=strlen ( $variable );
 $last_2nd=substr($variable,$len-2,1);
 if ($last_2nd==1)
 $last_2nd=2;
 $last=substr($variable,$len-1,1);
 $last=$last+(9-$last);
 $variable=substr($variable,0,$len-2).$last_2nd.$last;
 echo $variable;
?>
相关问题