Jump to content
Sign in to follow this  
jorgeng

Price problem

Recommended Posts

jorgeng

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

Share this post


Link to post
Share on other sites
Mat

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

Share this post


Link to post
Share on other sites
Zedna

And here is old dirty way :-)

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

Share this post


Link to post
Share on other sites
jorgeng

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

Share this post


Link to post
Share on other sites
jorgeng

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?

:)

Share this post


Link to post
Share on other sites
twitchyliquid64

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

Share this post


Link to post
Share on other sites
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?

:)

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

Share this post


Link to post
Share on other sites
Malkey

And here is a new cleaner way :-)

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

Edited by Malkey

Share this post


Link to post
Share on other sites
Mat

My ways cleaner :)

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

Share this post


Link to post
Share on other sites
Malkey

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

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

Share this post


Link to post
Share on other sites
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

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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.