Question Decimal issues in Word Notation

BigDumDum

New member
Joined
Mar 21, 2020
Messages
2
Programming Experience
Beginner
im new to coding and C# and wanted to make an idle game, i implemented a word notation system, which was far more frustrating than i wish it would have been, the result was that 1000 was displayed as 1K, which i wanted. but 1501 was displayed as 2K, rounded up, so i had to add two decimals preventing it from doing that, which is fine i prefer it with decimals anyway now that i think about it. but it also gives number below 1000 decimals, example 398.85 instead of 398, and i have no idea how to fix that, any help is highly appreciated
here is the code for the WordNotation iself
C#:
public string WordNotation(double number, string digits)
{
    double digitsTemp = Math.Floor(Math.Log10(number));
    IDictionary<double, string> prefixes = new Dictionary<double, string>()
    {
        {3, "K"},
        {6, "M"},
        {9, "B"},
        {12, "T"},
        {15, "q"},
        {18, "Q"},
        {21, "s"},
        {24, "S"},
        {27, "O"},
        {30, "N"},
        {33, "D"},
        {36, "U"},
        {39, "Du"},
        {42, "Tr"},
        {45, "Qa"},
        {48, "Qi"},
        {51, "Sex"},
        {54, "Sep"},
        {57, "Oc"},
        {60, "No"},
        {63, "V"}
    };
   
    double digitsEvery3 = 3 * Math.Floor(digitsTemp / 3);

    if (number >= 1000)
        return (number / Math.Pow(10, digitsEvery3)).ToString(digits) + prefixes[digitsEvery3];
   
    return number.ToString(digits);
}

and heres the values the WordNotation is applied to

C#:
SoulsClickValueText.text = "Tap\n+" + WordNotation(SoulsClickValue, "F2") + " Souls";
SoulsText.text = " " + WordNotation(souls, "F2");
 
Last edited by a moderator:
Firstly, why would you use a Dictionary<double, string> when all the keys are integers? The number of digits is always going to be a whole number so that should be a Dictionary<int, string>.
 
As for the issue, have you actually debugged your code, i.e. set a breakpoint and stepped through the code line by to examine the state at each step? You can also break complex lines into multiple and then see what each part is doing. That way, you can compare the actual behaviour to your expectations and pinpoint exactly where it goes awry and how. If you still can't solve the issue, at least you can provide us with more specific information. You might also consider reviewing your algorithm to make sure that the issue is not there. Write down the steps and then perform them manually on various data to see that you get the right results. You can then debug your code with the same data and see where its behaviour diverges from your manual calculation. This is how software development works. It's not just write code, run code, throw your hands up if you don't get the right result. You need to break it down and examine smaller and smaller parts until you identify the specific issue.
 
As for the issue, have you actually debugged your code, i.e. set a breakpoint and stepped through the code line by to examine the state at each step? You can also break complex lines into multiple and then see what each part is doing. That way, you can compare the actual behaviour to your expectations and pinpoint exactly where it goes awry and how. If you still can't solve the issue, at least you can provide us with more specific information. You might also consider reviewing your algorithm to make sure that the issue is not there. Write down the steps and then perform them manually on various data to see that you get the right results. You can then debug your code with the same data and see where its behaviour diverges from your manual calculation. This is how software development works. It's not just write code, run code, throw your hands up if you don't get the right result. You need to break it down and examine smaller and smaller parts until you identify the specific issue.
well i used <double, string> because my values that i wanted to apply the WordNotation on were all doubles
and i actually did not know about the debugging thing, never did that nor exactly know how to so ill have to look into that, thank you
 
well i used <double, string> because my values that i wanted to apply the WordNotation on were all doubles
You're not using the parameter as a key into the dictionary. You explicitly use Math.Floor to get a whole number from the parameter so your keys will always be whole numbers, so there's no reason to use Double.
 
Back
Top Bottom