Millisecond Forums

Getting values to display number up to two decimal points

https://forums.millisecond.com/Topic16479.aspx

By aghartley - 6/10/2015

Hello,

I am programming a task in which participants can earn money by guessing correctly. The cumulative money they have won is displayed in the corner of the screen so they can keep track of how much money they've made. I'd like the text to display they money they've made, with two zeros after the decimal point so it looks like a monetary value (e.g., $3.50), rather than what it shows now, which is just one place after the decimal ($3.5). How would I do this? Here's are some sample pieces of code that are relevant. Thanks in advance for your help!

**where I define value parameters:

<values moneyparams>
/ balance_total = 2.00
/ prevtotal = 0
/ currency = "$"
/ gainincentive = .50
/ loseincentive = .50
/ correctguess = 0
/ correctguessIn = 0
</values>

**where I define the text that displays the participant's balance.

<text balance_total>
/ items = ("<%values.currency%> <%values.balance_total%> ")
/ fontstyle = ("Verdana", 4.0%, true, false, false, false, 5, 0)
/ halign = left
/ hjustify = center
/ vjustify = center
/ size = (25%, 10%)
/ position = (80%, 16%)
/ txcolor = (green)
/ txbgcolor = (black)
/ erase = false
</text>
By Dave - 6/10/2015

You can use the the format() function to do that:

<values moneyparams>
/ balance_total = 2.00
/ prevtotal = 0
/ currency = "$"
/ gainincentive = .50
/ loseincentive = .50
/ correctguess = 0
/ correctguessIn = 0
</values>

**where I define the text that displays the participant's balance.

<text balance_total>
/ items = ("<%values.currency%> <%format(~"%.2f~", values.balance_total)%> ")
/ fontstyle = ("Verdana", 4.0%, true, false, false, false, 5, 0)
/ halign = left
/ hjustify = center
/ vjustify = center
/ size = (25%, 10%)
/ position = (80%, 16%)
/ txcolor = (green)
/ txbgcolor = (black)
/ erase = false
</text>

<trial mytrial>
/ ontrialbegin = [values.prevtotal=values.balance_total]
/ ontrialend = [values.balance_total=values.prevtotal+values.gainincentive]
/ stimulusframes = [1=balance_total]
/ validresponse = (57)
</trial>

<block myblock>
/ trials = [1-5=mytrial]
</block>