0

Change price to always show as: 29, 39, 49, 59, 69, 79, 89 or 99.
Eg: if price is 2018 = 2029
Or if price is 1051 then markup to: 1059

6
  • So you want the number to always end in 9?
    – Darren
    Commented Jul 29, 2014 at 4:59
  • 3
    Why would 2018 go to 2029 and not 2019? Commented Jul 29, 2014 at 5:00
  • round($num, - 1) - 1; lol
    – Darren
    Commented Jul 29, 2014 at 5:02
  • price fix display more then or equal 29 last digit.
    – HK_Khunt
    Commented Jul 29, 2014 at 5:02
  • So, have you tried anything yourself already? Commented Jul 29, 2014 at 5:05

6 Answers 6

4

Seems simple enough. Separate the number into two parts (pseudo-code below):

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

Then adjust the remainder with something like:

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

Then recombine them:

number = hundreds + remainder

You could simplify the middle bit into an expression if you so desire but, depending on the business logic, it may be better off leaving as-is, in case the boundaries become .29, .37, .53 (or anything not immediately amenable to a formula).


By way of example, here's some Python code (the ultimate pseudo-code language) which does what you want:

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))

The output is as expected:

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
3
  • 1
    this is not working properly. When i am number input 1562 it's return 6299.
    – HK_Khunt
    Commented Jul 29, 2014 at 5:14
  • @user3223673: I suugest you try to implement the code again. With 1562 as input, remainder gets set to 62 and hundreds becomes 1500. The remainder should then be upgraded to 69 and recombined to give 1569 (as per my sample code). If it doesn't, your problem lies with the translation om my pseudo-code to your language of choice.
    – paxdiablo
    Commented Jul 29, 2014 at 5:27
  • Another twist in comments, now decimal values are also in the picture: stackoverflow.com/questions/25008485/… Commented Jul 29, 2014 at 5:41
2

In case of floating number, it won't work.

You first need to check whether value is integer (decimal) or float. And after that, you need to process logic accordingly.

make a function like below:

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;
}

Regards

2
  • It works with nominal changes! Thanks!
    – HK_Khunt
    Commented Jul 29, 2014 at 5:55
  • by the way, $n will be replaced by $price. please note that
    – HK_Khunt
    Commented Jul 29, 2014 at 5:56
2

need to compare last 2 values using substr function as shown above. then check condition for nearest possible values. there you go. DONE .

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

AND SO ON....

CHEERS...

3
  • The 'and so on' is not necessary. There is no need for a long list else if statements. See my answer for a simpler way to do this. Commented Jul 29, 2014 at 5:27
  • 1
    Now there is another twist. What about the decimal values ? :) They just mentioned stackoverflow.com/questions/25008485/… Commented Jul 29, 2014 at 5:40
  • when $variable price 3635.50 will it be 3639.9 ? @HK_Khunt Commented Jul 29, 2014 at 5:48
2

Try this one:

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

see demo Updated with multiple examples


Update with suggestion from @HankyPanky in comments. For more simplicity, use the ternary operator:

$price = substr($price,-2) >= 20 ? substr($price,0,-1) . 9 : substr($price,0,-2) . 29;
3
  • @Hanky웃Panky That's what I thought at first but re-read the OPs question and his comment (#4). Any number less than x29 should be rounded to 29. Note the wording of the question: Change price to always show as: 29, 39, 49, 59, 69, 79, 89 or 99. It does not include 09 or 19. Commented Jul 29, 2014 at 5:23
  • Mark may I suggest adding this option to your neat answer. $price=substr($price, -2) >= 20 ? substr($price, 0, -1) . 9 : substr($price, 0, -2) . 29; Commented Jul 29, 2014 at 5:38
  • @Hanky웃Panky Yes that's a great suggestion. I have added your input to the answer. Thanks! Commented Jul 29, 2014 at 5:42
1

You can do it by making your php function with the use of round function of PHP

0

It is working fully. I have tested in my local server.

<?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;
?>
3
  • Hello when $variable price 3635.50 it's return 3635.9 but i want this 3639.9
    – HK_Khunt
    Commented Jul 29, 2014 at 5:36
  • That's another complicated job. But have to sole in this way as I calculated. Do you know programming? @HK_Khunt . Thanks for testing my answer. Commented Jul 29, 2014 at 5:41
  • If $variable price 3635.57 what will be your answer? @HK_Khunt Commented Jul 29, 2014 at 5:43

Not the answer you're looking for? Browse other questions tagged or ask your own question.