Search Unity

Handle Number Prefix

Discussion in 'Scripting' started by domdev, Apr 14, 2020.

  1. domdev

    domdev

    Joined:
    Feb 2, 2015
    Posts:
    375
    I was wondering how to handle number prefix with large numbers, I was checking this game civwar, and I like how it handle the numbers, like even it was already in millions, eg. it show 50103.8k which I think it was 50.1 million.. any body know how to do that? I tried searching in formating but I cant find which it range to 4 digit with decimal
     
  2. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    You do this by successively looking to find the biggest unit that your number fits, and tehn divide the original number by that order of magnitude. You'll recevie a string back, not a number.

    For example ()

    Code (CSharp):
    1. string adaptMagnitude(float inNumber) {
    2.    if (inNumber >= 1000000.0f) return (inNumber/100000.0f).ToString("F1") + "M";
    3.    if (inNumber >= 1000.0f) return (inNumber/1000.0f).ToString(F1)+ "K";
    4.    etc.
    5. }
     
  3. domdev

    domdev

    Joined:
    Feb 2, 2015
    Posts:
    375
    will try this one, thank you so much