Jump to content



Photo

Price problem


  • Please log in to reply
10 replies to this topic

#1 jorgeng

jorgeng

    Adventurer

  • Active Members
  • PipPip
  • 121 posts

Posted 18 July 2011 - 02:59 PM

Hello.
I have used this code which works great when price is 1021.00, 1021.25, 1021.50, 1021.75 and 1022.00.
Now i get strange price from ticket-delivery trading firm meaning prices as 1021.15, 1021.35, 1021.55.
This ticket-size shouldn't be able to exist but they do so i have to code something which delivers only
output courses as 1021.00, 1021.25, 1021.50, 1021.75 and 1022.00.

This is swedish crona which only shall have this 25 öre ticket sizes.

I have to code something as only give output at even 25 öre meaning that
if price is 1021.00 -> 1021.24 the output price should be 1021.00
if price is 1021.25 -> 1021.49 the output price should be 1021.25
if price is 1021.50 -> 1021.74 the output price should be 1021.50
if price is 1021.75 -> 1021.99 the output price should be 1021.75
if price is 1022.00 -> 1021.24 the output price should be 1022.00

Can anyone help me coding this..


; Kolla om Köp
$result_ObiWan = StringInStr($var_original, "Entry Long")
If $result_ObiWan < 43 And $result_ObiWan > 0 Then
; Lägg på 0.5 punkter om Köp trade
; $sStr = '15:47 ORDER "sl) Omx ObiWan - Köp Trade - Grön - Krypterat OMXS300G" kurs 1021.00'
$a=StringRegExp($new,"kurs (\S*)",1)
$b = $a[0] + .50
If Not StringInStr($b,".") Then $b &= ".00"
$c = StringReplace($new,$a[0],$:)
; ConsoleWrite($c & @LF)
EndIf

Edited by jorgeng, 18 July 2011 - 03:00 PM.








#2 Mat

Mat

    43 38 48 31 30 4E 34 4F 32

  • MVPs
  • 4,101 posts

Posted 18 July 2011 - 03:08 PM

For that conversion you can do: Floor(Original * 4) / 4

e.g.

$fOriginal = 1021.20 ; Should go to 1021.00 $fNew = Floor($fOriginal * 4) / 4 ConsoleWrite($fOriginal & " -> " & $fNew & @LF)

(above code untested)

Mat

I don't know where I'm going, but I'm on my way.

AutoIt Project Listing


#3 Zedna

Zedna

    AutoIt rulez!

  • MVPs
  • 8,409 posts

Posted 18 July 2011 - 03:35 PM

And here is old dirty way :-)

AutoIt         
For $i = 1 To 20     $number = Round(Random(0,1000),2)     ConsoleWrite($number & '-->' & Round025($number) & @CRLF) Next Func Round025($n)     $n1 = Floor($n)     $n2 = Round(Mod($n, $n1),2)     Switch $n2         Case 0 To 0.24             $n3 = 0         Case 0.25 To 0.49             $n3 = 0.25         Case 0.5 To 0.74             $n3 = 0.5         Case 0.75 To 0.99             $n3 = 0.75 ;~      Case Else ;~          $n3 = '??'     EndSwitch     Return StringFormat('%.2f', $n1 + $n3) EndFunc


#4 jorgeng

jorgeng

    Adventurer

  • Active Members
  • PipPip
  • 121 posts

Posted 18 July 2011 - 03:43 PM

For that conversion you can do: Floor(Original * 4) / 4

e.g.

$fOriginal = 1021.20 ; Should go to 1021.00 $fNew = Floor($fOriginal * 4) / 4 ConsoleWrite($fOriginal & " -> " & $fNew & @LF)

(above code untested)

Mat

Thanks, works great...

#5 jorgeng

jorgeng

    Adventurer

  • Active Members
  • PipPip
  • 121 posts

Posted 19 July 2011 - 05:55 AM

For that conversion you can do: Floor(Original * 4) / 4

e.g.

$fOriginal = 1021.20 ; Should go to 1021.00 $fNew = Floor($fOriginal * 4) / 4 ConsoleWrite($fOriginal & " -> " & $fNew & @LF)

(above code untested)

Mat

It seems like the result is 1021.0 from 1021.20
and 1021.5 from 1021.55, not 1021.50 as i want
How do i do to get two digits?

:)

#6 twitchyliquid64

twitchyliquid64

    Peace. Always.

  • Active Members
  • PipPipPipPipPipPip
  • 520 posts

Posted 19 July 2011 - 06:24 AM

It seems like the result is 1021.0 from 1021.20
and 1021.5 from 1021.55, not 1021.50 as i want
How do i do to get two digits?

:)


I think you can use The StringFormat Function to force a certain number of digits in a string. Im not sure of its parameters, however, it can be found in the helpfile.
ongoing projects:-firestorm: Largescale P2P Social NetworkCompleted Autoit Programs/Scripts: Variable Pickler | Networked Streaming Audio (in pure autoIT) | firenet p2p web messenger | Proxy Checker | Dynamic Execute() Code Generator | P2P UDF | Graph Theory Proof of Concept - Breadth First search

#7 Mat

Mat

    43 38 48 31 30 4E 34 4F 32

  • MVPs
  • 4,101 posts

Posted 19 July 2011 - 07:00 AM

It seems like the result is 1021.0 from 1021.20
and 1021.5 from 1021.55, not 1021.50 as i want
How do i do to get two digits?

:)

Zedna demonstrated it in his above post: StringFormat('%.2f', $fNew)

I don't know where I'm going, but I'm on my way.

AutoIt Project Listing


#8 Malkey

Malkey

  • Active Members
  • PipPipPipPipPipPip
  • 1,304 posts

Posted 19 July 2011 - 09:42 AM

And here is a new cleaner way :-)

AutoIt         
For $i = 1 To 20     $number = Round(Random(0, 1000), 2)     ConsoleWrite($number & '-->' & Round025($number) & @CRLF) Next Func Round025($n)     Local $n1 = Floor($n)     Return StringFormat('%.2f', $n1 + Int(Round(Mod($n, $n1), 2) * 4) / 4) EndFunc   ;==>Round025 #cs     Expanded Zedna's $n2 = Round(Mod($n, $n1), 2)     to           $n2 = Int(Round(Mod($n, $n1), 2) * 4) / 4         Let $n = 123.49     Returns 0.49        Round(Mod($n, $n1),2)                   ; Start     Returns 49          Round(Mod($n, $n1),2) *100              ; * 100 (For Int() to work next line, must be integer)     Returns 1       Int(Round(Mod($n, $n1),2) *100 /25)         ; Number on 25's present     Returns 25      Int(Round(Mod($n, $n1),2) *100 /25) * 25    ; Only return 0, 25, 50, or 75.     Returns 0.25    Int(Round(Mod($n, $n1),2) *100 /25) * 25 / 100 ; Only return 0.00, 0.25, 0.50, or 0.75 (/100 returns back to a decimal)     Returns 0.25    Int(Round(Mod($n, $n1),2) *   4   ) / 4     ; 100 /25 = 4 and  " * (25 / 100)"  = " / 4" #ce

[/quote]

Edited by Malkey, 19 July 2011 - 09:50 AM.


#9 Mat

Mat

    43 38 48 31 30 4E 34 4F 32

  • MVPs
  • 4,101 posts

Posted 19 July 2011 - 10:12 AM

My ways cleaner :)

Func Round025($n)     Return StringFormat('%.2f', Floor($n * 4) / 4) EndFunc   ;==>Round025

Edited by Mat, 19 July 2011 - 10:12 AM.

I don't know where I'm going, but I'm on my way.

AutoIt Project Listing


#10 Malkey

Malkey

  • Active Members
  • PipPipPipPipPipPip
  • 1,304 posts

Posted 19 July 2011 - 12:16 PM

Mat

You are correct.
"Floor($n * 4) / 4" is almost spotless.

But, from habit, I prefer "Int($n * 4) / 4".

And Int() is slightly faster than Floor().
Results:
Int($fOriginal * 4) / 4 ; Time (ms) 54.98 to 83.88 Average = 57.3
Floor($fOriginal * 4)/4 ; Time (ms) 56.21 to 88.51 Average = 58.46
AutoIt         
Local $fOriginal = 1021.99 ; Should go to 1021.00 Local $max, $min, $fNew, $dif, $sum ; --- Initialize $max & $min ---- Local $begin = TimerInit() For $i = 1 To 10000     $fNew = Int($fOriginal * 4) / 4     ;$fNew = Floor($fOriginal * 4)/4 Next $dif = TimerDiff($begin) $max = $dif $min = $dif $sum = $dif ; ---> End of Initialize $max & $min ---- For $x = 1 To 99     $begin = TimerInit()     For $i = 1 To 10000         $fNew = Int($fOriginal * 4) / 4   ; Time (ms) 54.98 to 83.88  Average = 57.3         ;$fNew = Floor($fOriginal * 4)/4  ; Time (ms) 56.21 to 88.51  Average = 58.46     Next     $dif = TimerDiff($begin)     If $dif > $max Then $max = $dif     If $dif < $min Then $min = $dif     $sum += $dif Next ConsoleWrite("Time (ms) " & Round($min, 2) & " to " & Round($max, 2) & "  Average = " & Round($sum / 100, 2) & @LF)


If you had not put "(above code untested)", in post #2, I would have looked closer your example, and would not have posted in this thread. And would have not wasted my time comparing the functions Int() and Floor() which has most likely been done before, recently.

Malkey

#11 Mat

Mat

    43 38 48 31 30 4E 34 4F 32

  • MVPs
  • 4,101 posts

Posted 19 July 2011 - 01:03 PM

You are correct.
"Floor($n * 4) / 4" is almost spotless.

But, from habit, I prefer "Int($n * 4) / 4".

And Int() is slightly faster than Floor().
Results:
Int($fOriginal * 4) / 4 ; Time (ms) 54.98 to 83.88 Average = 57.3
Floor($fOriginal * 4)/4 ; Time (ms) 56.21 to 88.51 Average = 58.46

If you had not put "(above code untested)", in post #2, I would have looked closer your example, and would not have posted in this thread. And would have not wasted my time comparing the functions Int() and Floor() which has most likely been done before, recently.

Int is the correct one to use in this case from a technical perspective, as Int rounds towards zero and Floor rounds towards -infinity... Makes a difference with negative numbers :)

I have to put "(above code untested)" as I haven't tested it, and so I need an excuse when it doesn't work.

I don't know where I'm going, but I'm on my way.

AutoIt Project Listing





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users