Currency - Forcing 2 decimal places

0 favourites
  • 4 posts
From the Asset Store
Spooky Places game assets for 2D endless runner action adventure game.
  • My first post, and I'm new to both coding and makings games.

    I want to have 2 decimal places with my currency variable.

    Scenario: I've built a little clicker app, which uses currency, and I have picked Dollars as the currency of choice.

    A lot of my products are worth less than a whole Dollar and would break what I'm doing to use whole dollars, which means I need to use cents to 2 decimal places.

    The issue I came across is that when money earnt as a single integer, eg $7.80, it displays as $7.9 - some people are thinking this is 9cents but also, it just does feel nice. or if it is $7.00, this shows as "$7". How do I force 2 decimal places at all times?

    Im rounding to 2 decimal places on all transactions currently.

    Here is what I have to try force the decimal places when the text doesnt have one or both decimal places visible:

    "$" & (round(Cash * 100) / 100) & (Cash % 1 = 0 ? ".00" : "")

    The forum hasnt been great for searching, or I dont know how to use it. Tutorial doesnt jump out with anything helpful.

    Any skilled peeps out there with cool solutions for this?

    -T2

  • "$" & round(Cash * 100) / 100 & (Cash % 1 > 0 ? "." & round((Cash % 1) * 100) : ".00")

    You try "$" & round(Cash * 100) / 100 & (Cash % 1 > 0 ? "." & round((Cash % 1) * 100) : ".00")

    I'm a noob, so I can't guarantee it's correct.

  • Separate out the decimal portion and integer portion. Use zeropad() to add zeros for the decimal portion.

    For any number "n" - set text to int(n) & "." & zeropad(round((n-int(n))*100),2)

    To zeropad to "d" digits instead of 2, it would be int(n) & "." & zeropad(round((n-int(n))*10^d),d)

    int(n) - the whole number portion of n

    n-int(n) - the decimal portion of n

    round((n-int(n))*10^d) - multiplying the decimal portion of n to get a whole number up to d digits, and rounding to get rid of the remaining decimals

    zeropad(n,d) - pad number (n) out to a certain number of digits (d) by adding zeroes in front of the number

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • "$" & round(Cash * 100) / 100 & (Cash % 1 > 0 ? "." & round((Cash % 1) * 100) : ".00")

    You try "$" & round(Cash * 100) / 100 & (Cash % 1 > 0 ? "." & round((Cash % 1) * 100) : ".00")

    I'm a noob, so I can't guarantee it's correct.

    Thank you. A variation to this worked well: The math made some sense to me /shrug.

    Working Solution:

    "$" & floor(Cash) & "." & right("00" & floor(Cash * 100) % 100, 2)

    Also thank you oosyrag - your answer was tricky for me to figure out for my noobiness, IM learning a lot but 1 week in this broke my lil old brain. I plan to come back and see if it makes more sense to me later and hopefully cool for someone else wanting the same answer =D

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)