Having trouble with writing a method that will use strings and bigdoubles as the inputs.

Hi I have only been using Unity for a short time but have learnt a great deal. Now I am following a tutorial series specific to the type of game I want to make but the author does things in the least efficient way by repeating the same code several times that could be combined into one method and reused instead. I have managed to teach myself how to fix these things as I go but have come to a point where they have created strings then used those strings in the following code. I have attempted to create a method with this code but have not had any success with using strings as inputs for a method that are then used in the method. Any help with this would be appreciated as I am not sure where I am going wrong here.

public string productionUpgrade1CostString;
public BigDouble productionUpgrade1Cost;

public void Update()
    {  
         ScientificNotation(productionUpgrade1Cost, productionUpgrade1CostString);
    }
 
public void ScientificNotation(BigDouble input, string input2)
    {
        if (input > 1000)
        {
            var exponent = (BigDouble.Floor(BigDouble.Log10(BigDouble.Abs(input))));
            var mantissa = (input / BigDouble.Pow(10, exponent));
            input2 = mantissa.ToString("F2") + "e" + exponent;
        }
        else
        {
            input2 = input.ToString("F2");
        }
    }

I’m actually not sure what you’re attempting to do. If this is all under one script, there may not be a need to pass the field values amongst the methods. And even with that aside, you’re passing in input2 string value but don’t appear to be using it.

So… the question is, what are you trying to do?

I am trying to condense the original code into one method here is a copy of two parts of the original code but it is repeated for several variables and strings basically it goes on for several pages.

        string flasksPerSecondString;
        if (flasksPerSecond > 1000)
        {
            var exponent = (BigDouble.Floor(BigDouble.Log10(BigDouble.Abs(flasksPerSecond))));
            var mantissa = (flasksPerSecond / BigDouble.Pow(10, exponent));
            flasksPerSecondString = mantissa.ToString("F2") + "e" + exponent;
        }
        else
        {
            flasksPerSecondString = flasksPerSecond.ToString("F2");
        }

        string flasksString;
        if (data.flasks > 1000)
        {
            var exponent = (BigDouble.Floor(BigDouble.Log10(BigDouble.Abs(data.flasks))));
            var mantissa = (data.flasks / BigDouble.Pow(10, exponent));
            flasksString = mantissa.ToString("F2") + "e" + exponent;
        }
        else
        {
            flasksString = data.flasks.ToString("F2");
        }

Because of the way it is laid out I need to add in this whole section for each number I want to make display as scientific instead of calling a method with some variables.

Well, strings are value types, so you’re not going to get the effect you want. By setting the value to input2, you’re changing the value of input2 and not the original string. What I would do is have the method take only input1 and then return the string which you can assign to your variable instead of trying to pass it a value and a string like you’re doing.

I will give that a try thank you.

I was just looking at it all wrong thanks for the help the following code worked the way I wanted it to and cut out about 10 pages of code.

    public string ScientificNotation(BigDouble input)
    {
        if (input > 1000)
        {
            var exponent = (BigDouble.Floor(BigDouble.Log10(BigDouble.Abs(input))));
            var mantissa = (input / BigDouble.Pow(10, exponent));
            return mantissa.ToString("F2") + "e" + exponent;
        }
        else
        {
            return input.ToString("F2");
        }
    }