#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

# How to limit decimal places (or convert CM-IN neatly)

edited November 2015
Hi there. Using Twine 1.4.2

[EDIT]
fixed the decimal issue <<\$height>>cm <<print ((\$height * 0.39370 / 12).toFixed(2))>> (see first comment for more info)

I am still looking for a neater way to show height in CM and in feet... but I suspect that the simplest answer will be to enter the info in Inches and then convert to feet and CM...

Incidently, is it possible to divide the inch-result by 12, then subtract that result to leave the remaining inches.
EG: 121 inches becomes 10 and 1?
I'm going to keep grinding at it... but any assistance is appreciated. I'm very new to programming and am liable to make all kinds of space-taking mistakes haha
[/EDIT]

I am trying to show a characters height (\$height) and want to show a vague cm-in conversion. At least getting the right number of feet.
I'm currently using:
Height: <<\$height>>cm <<print \$height * 0.39370>>in

Trying to apply your <<set \$var3 to Number((\$var1 / \$var2).toFixed(1))>> has sadly fried my tiny little brain... Programming isn't something I am that skilled with (proud to have gotten this far) but I think it should be possible to at least divide the result of the 'inches' result by 12... but then I need to limit the decimal places.

Of course, I'd love to know if there's a better way of converting these numbers.
The 'player' enters the height themselves (two inches tall? ok. if you want...) and I want to be able to use it for comparisons later (larger than, less than. print 'you hooge!' or 'puny lill playerperson!')

thanks in advance for any help

• AH!
I have a fix for the decimal issue!
Managed to McGuyver a solution from: http://twinery.org/forum/discussion/comment/12948/#Comment_12948
<<\$height>>cm <<print ((\$height * 0.39370 / 12).toFixed(2))>>
(thanks to TheMadExile for the code)
• zededd wrote: »
Incidently, is it possible to divide the inch-result by 12, then subtract that result to leave the remaining inches.
EG: 121 inches becomes 10 and 1?
There's no integer division operator, so you'll have to use floating point division and then truncate the result to convert inches into whole feet:
```Math.trunc(\$inches / 12)
```
To find the remaining inches, you can use the remainder operator:
```\$inches % 12
```

Putting them both together might look something like the following: (assumes \$height is in inches)
```You are <<print Math.trunc(\$height / 12)>>&#x2032;<<print \$height % 12>>&#x2033;.
```
Which should look like the following: (assuming \$height is 121)
```You are 10′1″.
```

I'm sorry to say, that went a bit over my head...
I have managed to cobble this mess together:
Height: <<print ((\$heightin / 12).toFixed(0))>>'<<print \$heightin - ((\$heightin / 12).toFixed(0)) * 12>>" (<<print ((\$heightin * 2.54).toFixed(0))>>cm)
to output:
Height: 10'1" (307cm)

I was following the examples you provided up to "′".
I confess... I'm a little unsure about truncation as well (This, I put down to a lack in vocabulary). From what I understand, it means 'shorten by deleting the end', but I am not certain.

I have been reading through the wiki and can't find much on the 'math.' function...
Is there another place I should be looking?

thank you so much for replying, even if I didn't understand as much as I wanted to.
• edited November 2015
zededd wrote: »
I was following the examples you provided up to "′".
The character is the prime, is the double prime. In this use, they're stand-ins (abbreviations basically) for feet and inches. There's nothing special about their use there, you may replace them with whatever you wish.

zededd wrote: »
I confess... I'm a little unsure about truncation as well (This, I put down to a lack in vocabulary). From what I understand, it means 'shorten by deleting the end', but I am not certain.

I have been reading through the wiki and can't find much on the 'math.' function...
Is there another place I should be looking?
In this case, truncating a floating point number means to return the integral part of the number by removing any fractional digits (i.e. drop any fraction, returning only the whole/integer part).

Suggested reading: JavaScript reference @ MDN. Specifically, the Math object and, even more specifically, the Math.trunc() method.

Actually, looking at some of your example code, you must be using one of the Twine 1 vanilla story formats. In that case, you probably should not use Math.trunc() since those story formats do not guarantee its existence. Instead replace it with the Math.floor() method:
```Math.floor(\$inches / 12)
```
Which, for positive numbers, yields the same result as using Math.trunc() (one would think that it's safe to assume that the PC's height should always be positive).
• GAH! I tried to write
```&#x2032;
```
but it replaced it with an apostrophe. Honestly, I have no idea how to understand that kind of language.
```<<set \$var to "MakesPerfectSense">>
```
Is about the scope of my skill so far. when it comes to

I'm not using a template, but I am all but copy-and-pasting from the wiki...
These links are pretty heavy-going for me, but some of it makes sense! Thanks for the reading (and brain-melting) material. These might just keep me from making an even bigger fool of myself XD

I can't get to grips with Twine 2 and don't feel safe using it... so for now, I'm stuck in 1.4. But it seems to be suiting my learning-curve so far

(I have a question about adding 'dummy' text to a radio button. The button says "grab the gun" but the variable would be changed to "2" or something... Should I post another question, or can I pick your astonishing brain here?)
• zededd wrote: »
GAH! I tried to write
```&#x2032;
```
but it replaced it with an apostrophe. Honestly, I have no idea how to understand that kind of language.
Actually, it was replaced by the prime (as noted previously). It only vaguely looks like a right single-quote, which is visually similar to an apostrophe. The &#x2032; bit is an HTML numeric character reference, which are similar to HTML character entity references (e.g. &amp;, &quot;, and &nbsp;). The Unicode code point for the prime character in hexadecimal is 2032.

zededd wrote: »
(I have a question about adding 'dummy' text to a radio button. The button says "grab the gun" but the variable would be changed to "2" or something... Should I post another question, or can I pick your astonishing brain here?)
A new question would probably be better. We go off the rails plenty enough around here already.
• Oooooh! Html makes more sense now... I'll have to google how to get that to work for me.
Thankfully, I'm doing relatively well using the simpler (probably less powerful) stuff I am finding in the wiki.
Thanks for your help and patience
(I figured out the bulletpoints! Proud of myself now haha)