Calculation with values over 2 billion
I want to display the value from a double variable with an ui text and I'm using only double values. But after the calculations it displays somtimes negative values. Here is my code, but only the declaration and last method are relevant for my problem. It seems the calculated value is converted into an int anywhere but I can't find the reason.
baseProduction=2000000000
Level= between 1 and 500
[code language="c#"] using UnityEngine; using UnityEngine.UI; using System.Collections;
public class ProductionScr : MonoBehaviour {
public int baseProduction;
public float baseDuration;
public int baseCost;
public int environmentMalus;
public int environment;
public double realProduction;
public float realDuration;
public long realCost;
public int realEnvironmentMalus;
public float remainingTime;
public bool manager;
public bool produce;
private bool process;
public int Level;
public int productionID;
public GameObject valueHolder;
public Image processBalken;
public Text profit;
public Text unit;
void Start(){
UpdateStats ();
produce = false;
remainingTime = realDuration;
}
//increases Level
public void IncreaseLevel(){
Level += 1;
//valueHolder.GetComponent<ValueHolder> ().environment += environmentMalus;
UpdateStats ();
valueHolder.GetComponent<ValueHolder> ().UpdateAllStats (productionID);
}
public void Produce(){
if (process == false&& manager==false) {
StartCoroutine (Process ());
}
}
void Update(){
if (process==false && manager==true){
StartCoroutine (Process ());
}
remainingTime -= Time.smoothDeltaTime;
}
IEnumerator Process(){
process = true;
while (true) {
yield return new WaitForSeconds (0.001f);
processBalken.rectTransform.sizeDelta = new Vector2(((realDuration - remainingTime) / realDuration)*368*2-370,1);
if (remainingTime <= 0) {
remainingTime = realDuration;
Debug.Log ("Finished");
produce = false;
process = false;
valueHolder.GetComponent<ValueHolder> ().balance += realProduction;
yield return null;
}
}
}
public void UpdateStats (){
//Change Profit
environment = (valueHolder.GetComponent<ValueHolder> ().environment);
if (environment != 0) {
realProduction =System.Convert.ToDouble (baseProduction * Level * (1 - Mathf.Pow (1.000460523f,environment) / 100)); //Berechnet Produktion aus Level, Basis und ValueHolder.environment
} else {
realProduction =System.Convert.ToDouble (baseProduction * Level);
}
if (realProduction < 1000000) {
profit.text = realProduction.ToString ();
unit.text = "€";
}
else if (realProduction >= 1000000 && realProduction < 1000000000D) {
profit.text = (realProduction / 1000000).ToString();
unit.text = "Million €";
} else if (realProduction >= (double)1000000000D && realProduction < (double)1000000000000D) {
profit.text = (realProduction / (double)1000000000D).ToString();
unit.text = "Billion €";
} else if (realProduction >= (double)1000000000000D && realProduction < (double)1000000000000000D) {
profit.text = ((realProduction / (double)1000000000000D)).ToString();
unit.text = "Trillion €";
}
//Change Cost
realCost = Mathf.RoundToInt(System.Convert.ToSingle( baseCost * Level * Level* 3));
//Change Duration
realDuration =System.Convert.ToSingle( baseDuration / System.Math.Pow(2,Mathf.Floor (Level / 50)));
//Change Environmentmalus
realEnvironmentMalus = environmentMalus * Level;
//applies Environmentmalus
valueHolder.GetComponent<ValueHolder> ().environmentInfluence[productionID] = realEnvironmentMalus;
}
} [/code]
Answer by El__Nacho · Oct 03, 2015 at 03:58 PM
Oh I've found my problem xD the varibale baseProduction has to by of type double instead of int.
Your answer
Follow this Question
Related Questions
Calculate arithmetic problem from text 1 Answer
Null Reference Exception For An Unknown Reason 0 Answers
Help with making interactive text animation for a 2D game 0 Answers
3d Text update. How its work? 1 Answer
Text doesn't change with string C# 1 Answer