Changing prices after a certain point?
I'm prototyping a game similar to adventure capitalist and I've ran into a slight problem. Whenever my prices surpass 10,000,000, unity changes the price to 1.xxxxx e + 0x, instead of saying 10,234,502 for example. I want to setup something to say when the price passes 1,000,000 to change it to x.xxx million and then at 1,000,000,000, it will change to x.xxx billion, and so on. How would I go about setting this up? And I don't want to show decimals under 1,000,000.
Answer by Rotavele · Apr 13, 2016 at 05:47 AM
I have written a game similar to this, and I leave the cash alone so unity and the data goes smoothly.
Instead I use the game script (in update frame) to send the cash value to my currency converter script which displays the data such as "XX.xx Billion".
Basically you just need an if/else statement to check the amount and if it's over X,XXX,XXX,XXX then divide by 1,000,000 and alter the UI to display "XXX.xx Billions".
Also remember to start with the largest amount you wish to code in (Ex: Decillion) and if it's smaller, then use an else statement to check the next amount and just keep going down the line.
Answer by Redwolve · Apr 13, 2016 at 03:27 AM
my first thoughts on this would be to check for it to be >= to 1,234,567 then multiply by 1X10^(-3*counter) this counter will be how many time you have performed this already +1 so first time it will be 1 so it will multiply by .001 and the second .000001, etc.. This will give you 1,234.567 first time. Then Mathf.Round, or Floor, or Ceil which every you want to use. this will give you 1,234. Then multiply by .001 once more and you will get 1.234. keep a count of how many times this condition is met and create a case switch to add either + "million" or +"billion", etc.
I did have it setup like this:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class IncreasePrice : $$anonymous$$onoBehaviour
{
public float Price = 100;
public float increase$$anonymous$$ultiplier = 1.25f;
public Text priceText;
public float currentPrice;
int clicks;
Button priceButton;
void Start()
{
currentPrice = Price;
clicks = 0;
priceButton = gameObject.GetComponent<Button> ();
}
void Update()
{
currentPrice = currentPrice;
//priceText.text = "Price: " + currentPrice + "\n" + "Clicks: " + clicks;
if (currentPrice > 1000000)
{
currentPrice = currentPrice / 1000000;
priceText.text = "Price: " + currentPrice + "million";
}
else if (currentPrice < 1000000)
{
currentPrice = (int)currentPrice;
priceText.text = "Price: " + currentPrice;
}
}
public void OnClick()
{
currentPrice = currentPrice * increase$$anonymous$$ultiplier;
clicks += 1;
}
}
but it seemed to get to 10,000,000 and then just stay at one and wouldn't update the price with the multiplier.
i dont believe it would be causing your problem, however your dont have a condition for = to 1,000,000. i cant see anything wrong other than that. And what is the purpose of currentPrice = currentPrice; in your update? I'll keep looking for a problem though.
It might not be needed but after the OnClick function, I was making sure the price kept updated. Yeah it's probably not needed since the OnClick will update it automatically so
Your answer
Follow this Question
Related Questions
Why the decimals change when I move away from the object? 0 Answers
Float without comma 1 Answer
Calculate exact time passed after two decimal point (ex: 0.2143) 0 Answers
Dont round decimal places? 2 Answers
How to get type double to vector3? 0 Answers