How do I show money with correct punctuation?

Hi everyone.

I’ve made a vending machine and everything works fine but what the player sees is not what I would like.

I’m hoping someone or someones can help me do three things:

  1. include a period between the paymentdollars and paymentcents that doesn’t move the cents to the next line (I’ve tried [period]).
  2. get paymentcents to print as 00 rather than just 0 when there are no cents in the machine.
  3. when just a nickle is in the machine having the readout say 05 not just 5

NOTE: in Canada the $1 coin is called a “loonie” because it has a loon on it… a loon is a bird.

Chapter 2 - coins

the loonie is a thing.
the loonie is in the vending area.

check inserting loonie into coinslot:
	move the loonie to the coinbox;
	increase paymentdollars by 1;
	say "You put a loonie into the machine the readout says $[paymentdollars] [paymentcents]."

the quarter is a thing.
the quarter is in the vending area.

check inserting quarter into coinslot:
	move the quarter to the coinbox;
	increase paymentcents by 25;
	say "You put a quartrer into the machine the readout says $[paymentdollars] [paymentcents]."


the dime is a thing.
the dime is in the vending area.

check inserting dime into coinslot:
	move the dime to the coinbox;
	increase paymentcents by 10;
	say "You put a dime into the machine the readout says $[paymentdollars] [paymentcents]."


the nickle is a thing.
the nickle is in the vending area.

check inserting nickle into coinslot:
	move the nickle to the coinbox;
	increase paymentcents by 5;
	say "You put a nickle into the machine the readout says $[paymentdollars] [paymentcents]."
	
Chapter 3 - the mechanism

the coinbox is a container.

the coinbox is nowhere.
	
paymentdollars is a number that varies.

paymentdollars is 0.

paymentcents is a number that varies.

paymentcents is 0.

Instead of separate variables, I would use a unit!

Monetary value is a kind of value.
$1.99 specifies a monetary value with parts dollars and cents (optional, preamble optional).

Now Inform will understand things like “$5” and “$0.05” and so on, and will handle things like “$5.00 plus $0.05” (which will produce “$5.05” as expected).

1 Like

You can use the number specification as described in WI 15.15 for some of this.

Vending area is a room.

A dollar payment is a kind of value.
$99.99 specifies a dollar payment with parts dollars and cents.

Paymentdollars is a dollar payment that varies.

Chapter 2 - coins

the loonie is a thing.
the loonie is in the vending area.

carry out inserting loonie into something:
	move the loonie to the coinbox;
	increase paymentdollars by $1.00;

After inserting loonie into something:
	say "You put a loonie into the machine the readout says [paymentdollars]." instead.

the quarter is a thing.
the quarter is in the vending area.

carry out inserting quarter into something:
	move the quarter to the coinbox;
	increase paymentdollars by $0.25;

After inserting quarter into something:
	say "You put a quartrer into the machine the readout says [paymentdollars]." instead.

the dime is a thing.
the dime is in the vending area.

carry out inserting dime into something:
	move the dime to the coinbox;
	increase paymentdollars by $0.10;

AFter inserting dime into something:
	say "You put a dime into the machine the readout says [paymentdollars]." instead.

the nickle is a thing.
the nickle is in the vending area.

carry out inserting nickle into something:
	move the nickle to the coinbox;
	increase paymentdollars by $0.05;

After inserting nickle into something:
	say "You put a nickle into the machine the readout says [paymentdollars]." instead.
	
Chapter 3 - the mechanism

The coinslot is in the vending area. The coinslot is an open container.

The coinbox is in the waiting area. The coinbox is a closed, unopenable container.

Output:

Vending area
You can see a loonie, a quarter, a dime, a nickle and a coinslot (empty) here.

>insert loonie into coinslot
(first taking the loonie)
You put a loonie into the machine the readout says $1.00.

>insert dime into coinslot
(first taking the dime)
You put a dime into the machine the readout says $1.10.

>insert quarter into coinslot
(first taking the quarter)
You put a quartrer into the machine the readout says $1.35.

I’ve taken some liberties with the coinslot, box, and the action handling (rather than defining this all within a Check action, which helps with the mechanics of the action and what the player sees as a result). But aside from those liberties, the gist is that once you define a number specification, you have a lot of flexibility of using that exact specification in code. The specification here will ensure that zeros are printed when handling currency.

You will also see in the documentation that there are options that allow you to control whether or not the cents part of the currency is required. (Is $3 ok if $3.00 is not specified?) If you need this, then you will want to add (optional, preamble optional) to the specification.

2 Likes

To answer this separately from the currency unit question:

To say period -- running on: (- print "."; -).

This enables "[period]" as a substitution with no line break.

@zarf’s answer is perfectly valid for period substitutions, but I also want to share something that I learned from @kamineko on his blog that I thought was pretty cool when you want to insert a period as text substitution:

To say dot:
	say "[unicode 46]".

say "Here are three dots: [dot][dot][dot]"

I keep a collection these shorthand notations in a section of my code when I want to insert special text, em dashes, a question mark, and exclamation point, and so on. This would also include shorthands for roman type, italic type, paragraph break, etc.

1 Like

Thank you guys for your answers. I’ve incorporated both ideas already. I appreciate you taking the time to reply.