Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 rounding numbers in GUI.
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Bruski




PostPosted: Mon Jun 16, 2003 7:21 am   Post subject: rounding numbers in GUI.

anyone know how to round the numbers to two decimal places....usually its :6:2 second being the amount of decimal places to display but im new to the GUI. commands and got no clue as to where to put this...any suggestions Confused

code:

import GUI

var winID : int
winID := Window.Open ("graphics:650;300,nobuttonbar")

%Declare identifiers
var nameTextField, addressTextField, cityTextField, provinceTextField, postalTextField, costTextField, yes2,no2, yes1,no1, fillClearButton,statusButton, refreshButton, quitButton : int
var total, tax, cost, subTotal, GSTtot, PSTtot, discount, membership : real
var tot := ""
var sub := ""
var gst := ""
var pst := ""
var dis := ""
var mem := ""
var pos := ""
var fill := true
const gstTax:=0.07
const pstTax:=0.08
var draw : boolean := false
tax := 0.15
var memFee:=0
var disTotal:=0.1
var cdPrice:=10.49

%procedures for fields and buttons
procedure NameEntered (text : string)
   GUI.SetSelection (addressTextField, 0, 0)
   GUI.SetActive (addressTextField)
end NameEntered

procedure AddressEntered (text : string)
   GUI.SetSelection (cityTextField, 0, 0)
   GUI.SetActive (cityTextField)
end AddressEntered

procedure CityEntered (text : string)
   GUI.SetSelection (provinceTextField, 0, 0)
   GUI.SetActive (provinceTextField)
end CityEntered

procedure ProvinceEntered (text : string)
   GUI.SetSelection (postalTextField, 0, 0)
   GUI.SetActive (postalTextField)
end ProvinceEntered

procedure PostalEntered (text : string)
   GUI.SetSelection (costTextField, 0, 0)
   GUI.SetActive (costTextField)
end PostalEntered

procedure CheckBoxPressed (filled : boolean)
        if yes1= GUI.GetEventWidgetID then
           memFee:=0
           disTotal:=0.1
        elsif yes2= GUI.GetEventWidgetID then
          memFee:=5
          disTotal:=0.1
        else
          memFee:=0
          disTotal:=0
        end if
end CheckBoxPressed


procedure CostCD (numCD : string)
   subTotal :=strreal (numCD) * cdPrice
   sub := realstr (subTotal, 10)
   GSTtot :=subTotal * gstTax
   gst := realstr (GSTtot, 10)
   PSTtot :=subTotal * pstTax
   pst := realstr (PSTtot, 10)
   discount:=subTotal*disTotal
   dis:=realstr (discount, 10)
   membership:=memFee
   mem:=realstr (membership, 10)
   total := subTotal + discount + memFee + PSTtot + GSTtot
   tot := realstr (total, 10)
end CostCD

procedure emptyf (text : string)
   tot:= ""
   sub:= ""
   gst:= ""
   pst:= ""
   dis:= ""
   mem:= ""
   pos:= ""
end emptyf

procedure caculate ()
   draw := true
end caculate

colorback (102)
GUI.SetBackgroundColor (white)
cls

%declare fields & labels
nameTextField := GUI.CreateTextFieldFull (155, 250, 155, "", NameEntered, GUI.INDENT, 0, 0)
var nameLabel := GUI.CreateLabelFull (145, 250, "Name:", 0, 0, GUI.RIGHT, 0)

addressTextField := GUI.CreateTextFieldFull (155, 220, 155, "", AddressEntered, GUI.INDENT, 0, 0)
var addressLabel := GUI.CreateLabelFull (145, 220, "Address:", 0, 0, GUI.RIGHT, 0)

cityTextField := GUI.CreateTextFieldFull (155, 190, 155, "", CityEntered, GUI.INDENT, 0, 0)
var cityLabel := GUI.CreateLabelFull (145, 190, "City:", 0, 0, GUI.RIGHT, 0)

provinceTextField := GUI.CreateTextFieldFull (155, 160, 45, "", ProvinceEntered, GUI.INDENT, 0, 0)
var provinceLabel := GUI.CreateLabelFull (145, 160, "Province:", 0, 0, GUI.RIGHT, 0)

postalTextField := GUI.CreateTextFieldFull (265, 160, 45, "", ProvinceEntered, GUI.INDENT, 0, 0)
var postalLabel := GUI.CreateLabelFull (257, 160, "Zip Code:", 0, 0, GUI.RIGHT, 0)

costTextField := GUI.CreateTextFieldFull (155, 100, 155, "", CostCD, GUI.INDENT, 0, 0)
var costLabel := GUI.CreateLabelFull (145, 100, "Number of CDs:", 0, 0, GUI.RIGHT, 0)

var taxtf := GUI.CreateTextFieldFull (420, 130, 155, "", emptyf, GUI.INDENT, 0, 0)
var gtax := GUI.CreateLabelFull (410, 130, "Total: $", 0, 0,GUI.RIGHT, 0)

var subTotalf := GUI.CreateTextFieldFull (420, 250, 155, "", emptyf, GUI.INDENT, 0, 0)
var gsub := GUI.CreateLabelFull (410, 250, "Subtotal: $", 0, 0,GUI.RIGHT, 0)

var gstf := GUI.CreateTextFieldFull (420, 160, 55, "", emptyf, GUI.INDENT, 0, 0)
var ggst := GUI.CreateLabelFull (410, 160, "GST: $", 0, 0,GUI.RIGHT, 0)


var pstf := GUI.CreateTextFieldFull (520, 160, 55, "", emptyf, GUI.INDENT, 0, 0)
var gpst := GUI.CreateLabelFull (515, 160, "PST: $", 0, 0,GUI.RIGHT, 0)

var disf := GUI.CreateTextFieldFull (420, 190, 155, "", emptyf, GUI.INDENT, 0, 0)
var gdis := GUI.CreateLabelFull (410, 190, "Discount: $", 0, 0,GUI.RIGHT, 0)

var memf := GUI.CreateTextFieldFull (420, 220, 155, "", emptyf, GUI.INDENT, 0, 0)
var gmem := GUI.CreateLabelFull (410, 220, "Mem Fee: $", 0, 0,GUI.RIGHT, 0)

%clear textfield procedure
procedure RefreshPressed
    GUI.SetText (provinceTextField, "")
    GUI.SetText (nameTextField, "")
    GUI.SetText (addressTextField, "")
    GUI.SetText (cityTextField, "")
    GUI.SetText (postalTextField, "")
    GUI.SetText (costTextField, "")
    GUI.SetText (postalTextField, "")
    GUI.SetText (taxtf, "")
    GUI.SetText (subTotalf, "")
    GUI.SetText (gstf, "")
    GUI.SetText (pstf, "")
    GUI.SetText (disf, "")
    GUI.SetText (memf, "")
    GUI.Refresh
   
end RefreshPressed

%checkboxes for membership
yes2 := GUI.CreateCheckBox (223, maxy - 177, "Yes?",CheckBoxPressed)
no2 := GUI.CreateCheckBox (270, maxy - 177, "No?",CheckBoxPressed)
var buymember := GUI.CreateLabelFull (210, maxy - 177, "If no, purchase a membership?", 0, 0,GUI.RIGHT, 0)
yes1 := GUI.CreateCheckBox (223, maxy - 157, "Yes?",CheckBoxPressed)
no1 := GUI.CreateCheckBox (270, maxy - 157, "No?",CheckBoxPressed)
var member := GUI.CreateLabelFull (210, maxy - 157, "Do you have a D3 membership card?", 0, 0,GUI.RIGHT, 0)

%box around text
var f1 := GUI.CreateLabelledFrame (7, 80, 643, 290, GUI.INDENT,
    "D3 CD Clearance Sale")

%buttons
var quitBtn : int := GUI.CreateButton (150, 40, 100, "Quit", GUI.Quit)
var calcb : int := GUI.CreateButton (260, 40, 100, "Calculate", caculate)
var clearScrn : int := GUI.CreateButton (370, 40, 100, "Clear Screen", RefreshPressed)

loop
   if draw then
       GUI.SetText (taxtf, tot)
       GUI.SetText (subTotalf, sub)
       GUI.SetText (gstf, gst)
       GUI.SetText (pstf, pst)
       GUI.SetText (disf, dis)
       GUI.SetText (memf, mem)
       draw := false
   end if
   exit when GUI.ProcessEvent
end loop
Window.Close(winID)


thx again
Sponsor
Sponsor
Sponsor
sponsor
Andy




PostPosted: Mon Jun 16, 2003 9:27 am   Post subject: (No subject)

just round it ur self, multiply the number by 100, div it by 1 and then / it by 100
Homer_simpson




PostPosted: Mon Jun 16, 2003 11:19 am   Post subject: (No subject)

use my function to turn them into what you want
code:
function decimal (r : real, i : int) : real
    var rs := realstr (r, 1)
    var ts := ""
    var c := 1
    var c2 := 0
    var dec := false
    loop

        if rs (c) not= "." then
            ts += rs (c)
        else
            ts += "."
            dec := true
        end if
        exit when c = length (rs) or c2 = i
        c += 1
        if dec then
            c2 += 1
        end if
    end loop
    result strreal (ts)
end decimal

put decimal (32.231321, 2)
Bruski




PostPosted: Mon Jun 16, 2003 11:50 am   Post subject: (No subject)

im confused...where do i put that...?
Homer_simpson




PostPosted: Mon Jun 16, 2003 1:04 pm   Post subject: (No subject)

put
code:
function decimal (r : real, i : int) : real
    var rs := realstr (r, 1)
    var ts := ""
    var c := 1
    var c2 := 0
    var dec := false
    loop

        if rs (c) not= "." then
            ts += rs (c)
        else
            ts += "."
            dec := true
        end if
        exit when c = length (rs) or c2 = i
        c += 1
        if dec then
            c2 += 1
        end if
    end loop
    result strreal (ts)
end decimal


at the top of your program...
this is how the functionworks

decimal(num:real,dec:int)

num is the number that you want to round
and dec is the number of decimals you want it to have

so if you put:
put deimal(100.5555,1)
the out put will be 100.5
JayLo




PostPosted: Mon Jun 16, 2003 1:41 pm   Post subject: (No subject)

wait... i used frealstr to do something like this... i'll try and find an example.
Andy




PostPosted: Mon Jun 16, 2003 2:26 pm   Post subject: (No subject)

there is such an command? what does it do? too lazy to go to turing and type and press F9
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 7 Posts ]
Jump to:   


Style:  
Search: