Howdy, Stranger!

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

[Twine 2 - Harlowe] Can I print a number to 2 decimal places?

Hi all,

I am just starting out using Twine 2 (using story format Harlowe 2.0) and still getting hang of a few things. I will apologise immediately if I use the wrong terminology when referring to things. (Still learning).

I have generally been able to find answers to issues, however, I cannot seem to find if I can print the result of a variable to 2 decimal places, such as printing 13.25 instead of a whole number. The only methods that I can see in the wiki only show how to do a whole. Is there an option to 2 decimals?

Any guidance would be great :)

Comments

  • I cannot test this at the moment, however, something like the following should work—though Harlowe does surprise me on occasion:
    (print: $numberVariable.toFixed(2))
    
    NOTE: The variable or expression you're attempting to use with the toFixed() method must be an actual number value, a string containing numerals is not the same thing.
  • Thank you :)
    I have been using the string containing numerals, as this is what I found was used and taught in tutorials. But I guess they had been using whole numbers and I went with numbers with decimals, which doesn't seem to be as friendly.
    I will have to look at how to use number values next.
  • I don't know what tutorials you're referring to, however, numeric strings should never be used in place of actual numbers if they're what you actually need.

    An easy rule of thumb is if you ever plan on doing math with a value, then it should be a number. If no math will ever happen with a value, then using a numeric string is probably okay.
  • Thanks :) I do understand what you mean. At the moment I don't know how to set numbers properly.

    Currently I am seeing in tutorials (just random tutes on peoples blogs and on youtube) that show the following expressions: (this is just an example)

    Passage 1
    (set: $name to 5)

    Passage 2
    (set: $name to $name + 5)

    which now makes $name = 10.

    They have been using this for HP/gold increases/decreases (or other numbers). So I assumed that this would be okay.

    I do understand what you are saying about numerals variables vs math numbers. I just haven't come across how to set it in numbers instead of numeral string. As the wiki is not really clear enough for me at such a beginner level. I have tried what I thought it was trying to say, but have been incorrect.
  • Please use the code tag when posting code or markup—it's C on the editor bar.

    Shiegurl85 wrote: »
    Currently I am seeing in tutorials (just random tutes on peoples blogs and on youtube) that show the following expressions: (this is just an example)

    Passage 1
    (set: $name to 5)

    Passage 2
    (set: $name to $name + 5)

    which now makes $name = 10.
    That is the correct way to set a number. The addition is correct as well, though, in Harlowe you may also write that as:
    (set: $name to it + 5)
    
    Regardless. If that's what you've been doing, then you should be okay.


    For an example of what I meant by a numeric string:
    → 1. These are numbers
    (set: $thingA to 5.6666)
    (set: $thingB to 5)
    
    → 2a. These are strings
    (set: $thingC to 'whazzat')
    (set: $thingD to "How now, brown cow.")
    
    → 2b. These are also strings (numeric strings)
    (set: $thingE to '5.6666')
    (set: $thingF to "5")
    
    My earlier warning was about the final set (2b). I cannot count the number of times we've seen people attempting to do math on (numeric) strings like that because they simply didn't know the difference.


    As an example of the <Number>.toFixed() method in action: (tested in both Harlowe v1.2.3 & v2.0.0)
    {
    	(set: $thingA to 5.6666)
    	(set: $thingB to 5)
    }\
    $thingA vs. (print: $thingA.toFixed(2))
    $thingB vs. (print: $thingB.toFixed(2))
    
    Which outputs the following:
    5.6666 vs. 5.67
    5 vs. 5.00
    
  • If trailing zeroes are not required, and if you don't need it too often, you can do some simple arithmetic:
    (set: $x to (floor: 100*it)/100)$x
    
    (print: $numberVariable.toFixed(2))
    
    Wow, so JavaScript can be used in Harlowe code!!!?!?!!? My world just grew exponentially! You made my day!
  • corfou wrote: »
    Wow, so JavaScript can be used in Harlowe code!
    Some Javascript can be used in Harlowe, the trick is to determine what works and what doesn't.
  • Please use the code tag when posting code or markup—it's C on the editor bar.

    Apologies


    For an example of what I meant by a numeric string:
    → 1. These are numbers
    (set: $thingA to 5.6666)
    (set: $thingB to 5)
    
    → 2a. These are strings
    (set: $thingC to 'whazzat')
    (set: $thingD to "How now, brown cow.")
    
    → 2b. These are also strings (numeric strings)
    (set: $thingE to '5.6666')
    (set: $thingF to "5")
    
    My earlier warning was about the final set (2b). I cannot count the number of times we've seen people attempting to do math on (numeric) strings like that because they simply didn't know the difference.


    As an example of the <Number>.toFixed() method in action: (tested in both Harlowe v1.2.3 & v2.0.0)
    {
    	(set: $thingA to 5.6666)
    	(set: $thingB to 5)
    }\
    $thingA vs. (print: $thingA.toFixed(2))
    $thingB vs. (print: $thingB.toFixed(2))
    
    Which outputs the following:
    5.6666 vs. 5.67
    5 vs. 5.00
    


    Thank you.
    Yes, I had been using them as numbers. Apologies, I knew I would get terminologies mixed up.

    This has been an amazing help thank you :)
Sign In or Register to comment.