I am looking to use a piece of code to take an extremely long number and convert it to decimal equivalent + short scale. (i.e. 120100000000 would be 120.1 Billion)

I am doing this with two separate scripts but this can be condensed into a single one. I am trying to look for a recurring process instead of the massive amount of if statements currently running in the script. This is being done with an external array in Unity that holds the strings for short scale. (billion,trillion, etc)

Trying to find a recurring model that will look for the length, and attach it to the appropriate array object.

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class amountText : MonoBehaviour {
    public Text text;
    public Text numberPref; 
    public string[] values;
    public static float number = 1000f;

    private float numberLength;
    private float remainder;
    private float increaseAmount = 5000f;

void Start () {
    text = GetComponent <Text>(); 
void Update () {
    text.text = Mathf.RoundToInt(number).ToString();
    numberLength =  Mathf.RoundToInt(number).ToString().Length;
    if(numberLength <9) {
        text.text = Mathf.RountToInt(number).ToString();
        numberPref.text = (" ");
    if(numberLength > 9 && numberLength =< 12) {
        text.text = number/1E-9;
        numberPref.text = (values[0]);
    if(numberLength > 12 && numberLength =< 15) {
        text.text = number/1E-12;
        numberPref.text = (values[1]);
    if(numberLength > 15 && numberLength =< 18) {
        text.text = number/1E-15;
        numberPref.text = (values[2]);


I would make only minor changes to your code:

private const string[] _postfixes = { "billion", ... };
public static string BigNumberToString(double number)
  int exponent = 0;
  double num = number;
  while(num > 1000.0)
    exponent ++;
    num /= 1000.0;
  //additional logic for converting here, for your example
  if(exponent < 3)
    return number.ToString();
    return num.ToString() + _postfixes[exponent - 3];

The while cycle is effectively the same like yours Mathf.RoundToInt(number).ToString().Length; and feel free to replace it if you don't need to parse numbers above int.MaxValue (the remainder would be exponent - (exponent % 3)). exponent - 3 because you are not interested in naming numbers below 1.0E+09, providing you direct index in your posfix array. You might also consider adding some error checking. Also please note I did not check the syntax.
sorry for the edits, it was not so straight-forward to implement as I thought

  • \$\begingroup\$ Much more elegant than I had originally created. I'll have to test it to ensure it will work for my intention, but much more streamlined than my original code. \$\endgroup\$ – Rj Joplin Aug 22 '15 at 18:42
  • \$\begingroup\$ @Rj Joplin Just if you happen to find any bug in the code, make sure to report it back so I could fix the answer. \$\endgroup\$ – wondra Aug 22 '15 at 18:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.