Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How to Create a personalized kind of ".ToString()" with parameters ?

Discussion in 'Scripting' started by Bakudan, May 14, 2015.

  1. Bakudan

    Bakudan

    Joined:
    Mar 29, 2015
    Posts:
    16
    Hello there^^.

    I'm not sure the description of the thread is exact but basically what i try to do is to create something like this (in C#) :

    Code (CSharp):
    1. float number = 50000000;
    2. string tmpNumber = "";
    3.  
    4.    method .ToString("Optimized")
    5. {
    6.     if (number > 1000){
    7.         tmpNumber = (int)(number/1000)+" Mega";
    8.     }
    9.     if (number > 1000000){
    10.         tmpNumber = (int)(number/1000000)+" Giga";
    11.     }
    12.     if (number > 1000000000){
    13.         tmpNumber = (int)(number/1000000000)+" Tera";
    14.     }
    15.     // display that tmpNumber instead of Number
    16. }
    17.  
    18. //textNumber = number.ToString("Optimized");
    19. Debug.Log(number.ToString("Optimized"));
    20. // i should see   " 50 Giga"
    I want to automatically with the .StringOptimized, display string depending of their lenght, for Display Damages purpose.
    The thing is i have no idea about how to create in Unity something like this personnalized .Component, as a .ToString() or so.

    If someone can enlight me about this i would be really gratfull (i'm not asking for my complete component but at least a way to create some personnalized .Component close to this^^.
    Thanks.

    Baku.
     
    Last edited: May 14, 2015
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    not a js user... so there might be typos in here but something like this:

    Code (csharp):
    1.  
    2. //unityscript
    3.  
    4. function ToStringOptimized(float f) : string
    5. {
    6.     string tmpNumber = "";
    7.  
    8.     if (f > 1000)
    9.     {
    10.         tmpNumber = (int)(f/1000)+" Mega";
    11.     }
    12.     if (f > 1000000)
    13.     {
    14.         tmpNumber = (int)(f/1000000)+" Giga";
    15.     }
    16.     if (f > 1000000000)
    17.     {
    18.         tmpNumber = (int)(f/1000000000)+" Tera";
    19.     }
    20.    
    21.     return tmpNumber;
    22. }
    23.  
    24.  
    25.  
    26. float number = 50000000;
    27. string textNumber = "";
    28. textNumber = ToStringOptimized(number);
    29. Debug.Log(textNumber);
    30.  
     
  3. CxydaIO

    CxydaIO

    Joined:
    May 12, 2014
    Posts:
    51
    hi, i'm also no JS user but i don't think you have to pass a float in JS! In c# in works like this:

    Code (CSharp):
    1. float number = 50000000;
    2. string tmpNumber = "";
    3.     public string ToOptimizedString()
    4. {
    5.     if (number > 1000){
    6.         tmpNumber = (int)(number/1000)+" Mega";
    7.     }
    8.     if (number > 1000000){
    9.         tmpNumber = (int)(number/1000000)+" Giga";
    10.     }
    11.     if (number > 1000000000){
    12.         tmpNumber = (int)(number/1000000000)+" Tera";
    13.     }
    14. return tmpNumber;
    15.     // display that tmpNumber instead of Number
    16. }
    hope this helps

    EDIT: uh but is see that you should use ELSE IF statements... otherwise every if statement is true for big numbers :)
     
  4. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    oh, good point... might be an idea to do this with a switch statement based on the "power" of the number rather than discrete if/if else statements


    edit: I actually had to look it up... my math teacher would be horrified! Mathf.Log10()

    http://docs.unity3d.com/ScriptReference/Mathf.Log10.html
     
    Last edited: May 14, 2015
  5. Bakudan

    Bakudan

    Joined:
    Mar 29, 2015
    Posts:
    16
    Ho i'm sorry it was supposed to be a C# script, i said "Function" because its not a method but something else i though.
    But i'ts not really a method function i try to do, its a permanent ".ToStringOptimized" i want to add in Unity to be able to call it in any script like we can use a .ToString().

    I basically know how to convert a string like this with a normal method, but i want to create/add in for exemple "
    UnityEngine;
    or System.Collections;
    or System.Collections.Generic;"
    Add this .ToStringOptimized as a permanent attribut i can use everywhere, not calling a method everytime, if you see.



    I'm gonna try your solutions see if it works and get back to you, thanks a lot^^.
     
    Last edited: May 14, 2015
  6. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    you can use "ToString()" on objects because they have implemented that function. It's not one "central" function.

    You could look into setting up a static class with this as a static function (pass number by parameter in that case). You'd only need to set that up once in your project

    i.e.
    Code (csharp):
    1.  
    2. SomeStaticClass.ToOptimizedString(number);
    3.  
    then it's like using Mathf.



    The interface that needs to be implemented for ToString
    https://msdn.microsoft.com/en-us/library/system.iformattable(v=vs.110).aspx
     
  7. CxydaIO

    CxydaIO

    Joined:
    May 12, 2014
    Posts:
    51
    Hey Bakudan, my solution should work for you, you just have to implement the ToStringOptimized() method i wrote in every class where you want to use it. but i think this should only be 1 or just a few. It's the same as the ToString() Method... it is implemented in every class like int, float etc
     
  8. Bakudan

    Bakudan

    Joined:
    Mar 29, 2015
    Posts:
    16
    Yes it works perfectly indeed, with you Solutions PBeast and LeftRighty i made this :
    Code (CSharp):
    1.  
    2.     public static string ToStringOptimized(float f,int d)
    3.     {
    4.         string tmpNumber = "";
    5.         string Decimal = "F" + d.ToString ();
    6.         if (f > 1000) {
    7.             tmpNumber = (f / 1000).ToString (Decimal) + " Mega";
    8.         } else if (f > 1000000) {
    9.             tmpNumber = (f / 1000000).ToString (Decimal) + " Giga";
    10.         } else if (f > 1000000000) {
    11.             tmpNumber = (f / 1000000000).ToString (Decimal) + " Tera";
    12.         } else {
    13.             tmpNumber = f.ToString ("F0");
    14.         }
    15.        
    16.         return tmpNumber;
    17.     }
    18.     }
    I was in fact missing the "return" and "string method" stuff, i'm just beginning at coding^^.

    And i'm now using this in a public static states so i can use it all along my scripts, thanks a lot again, i can now display my damages without insanely long numbers haha (i'm doing a little HeroesClicker/TapTitan project for fun)

    (below the current mini game project screen, huhu)
     
    Last edited: May 14, 2015