-4

I am using the following code to format currency in my unity game. But there is some error messages saying "Cannot explicitly convert string to float"

    public float FormatAmount(float amount)
    {
        if (amount <= 999) {
            return amount;
        }
        // Thousands 106
        else if (amount >= 1000 && amount <= 999999) {
            return (amount / 1000 + "K");
        }
        // Millions 106
        else if (amount >= 1000000 && amount <= 999999999) {
            return (amount / 1000000 + "M");
        }

        // Billions 109
        else if (amount >= 1000000000 && amount <= 999999999999) {
            return (amount / 1000000000 + "B");
        }

        // Trillions 1012
        else if (amount >= 1000000000000 && amount <= 999999999999999) {
            return (amount / 1000000000000 + "T");
        }

        // Quadrillions 1015
        else if (amount >= 1000000000000000 && amount <= 999999999999999999) {
            return (amount / 1000000000000000 + "Quad");
        }
        // Quintillions 1018
        else if (amount >= 1000000000000000000 && amount <= 999999999999999999) {
            return (amount / 1000000000000000000 + "Quin");
        } else {
            return amount;
        }
    }
Arman
  • 31
  • 9
  • 2
    You are returning a string, but your method signature is to return a float. Stop appending a string to the return values. – maccettura Jun 18 '18 at 15:57
  • Or change return value to string if that is what you want. – PaulF Jun 18 '18 at 15:58
  • 3
    `(amount / 1000 + "K");`, `(amount / 1000000 + "M")` and so on. You can't do that. You are trying to add string to float. What do you expect to happen? – Programmer Jun 18 '18 at 15:58
  • Question, can you not use the culture variance for determining currency? – Greg Jun 18 '18 at 16:02
  • @Programmer: you can do that - the numeric part is automatically converted to a string - if amount is 1234 the result is "1.234K" – PaulF Jun 18 '18 at 16:05
  • @PaulF I thought OP wanted to add the string value to the float value – Programmer Jun 18 '18 at 16:06
  • 1
    @Programmer: In that case it would be wrong, but I read it as adjusting the value & appending a string suffix for a display output string. – PaulF Jun 18 '18 at 16:08
  • Then that's fine if that's the case – Programmer Jun 18 '18 at 16:09
  • I also spotted the final comparison is 1 9 short - should be _"amount <= 9999999999999999999"_. Also with values that big, float may not be suitable : _"FormatAmount(1234567891234567894)"_ results in "1.234568Quin" for float, "1.23456789123457Quin" for float & "1.234567891234567894Quin" for decimal - so if full accuracy is required the parameter should be passed as decimal. – PaulF Jun 18 '18 at 16:18
  • 1
    currency should always use `decimal` type – Rufus L Jun 18 '18 at 16:21
  • Also, if you are making an idle game and want stupid huge values, I suggest getting a `BigInteger` class like [this one](https://numerics.codeplex.com/). I use [this function](https://github.com/Draco18s/IdleArtificer/blob/master/Assets/draco18s/artificer/game/Main.cs#L839) to trim down to a maximum number of displayable digits. – Draco18s no longer trusts SE Jun 18 '18 at 19:36

2 Answers2

2

There are a couple of problems with the code that you have provided. The method signature is declared as having a float output:

public float FormatAmount(float amount)

The float is the output and is defined as a float. And the second float is the input and it required a float (this one is ok if you call the function while providing a float)

But, in your returns, you have

  1. return amount - which will pass a float.
  2. return (amount / 1000 + "K"); - which will pass a string.

There are 2 possibilities here.

  1. If you need a float as the output of the function, you need to only return a float. That means that you will not have the +"letter".
  2. If you want to keep the letters and thus return a string, you need to make sure that you are only passing a string. That means that you can keep return (amount / 1000 + "K");, but you will also need to add .toString() to the return where you have only a float. So in the end it will look like this:
public string FormatAmount(float amount)
    {
        if (amount <= 999) {
            return amount.toString();
        }
        else if (amount >= 1000 && amount <= 999999) {
            return (amount / 1000 + "K");
        }
    }

You might also want to take PaulF's comment into account regarding the limitations of float, and Draco18's solution of using an external numeric library for values that large.

AlexT
  • 2,524
  • 1
  • 11
  • 23
  • This is not the issue. – Programmer Jun 18 '18 at 15:59
  • Care to explain how is that not the issue? He declared a float as the return param, and the tries to return a string. Either he gets ring of the +"String" or changes the return type. Or am I wrong ? – AlexT Jun 18 '18 at 16:02
  • If OP does require a string returning - that does actually solve the problem other than the two "return amount;" needs to change to "return amount.ToString();" – PaulF Jun 18 '18 at 16:02
  • PaulF made a comment I wanted to make. It depends on what wants to returning and simply doing what's in your answer wouldn't solve all the problem. – Programmer Jun 18 '18 at 16:04
  • Considering that OP is saying that the purpose is to "format currency". It is ok (I believe) to assume that he does actually want a string. edit: As to "wouldn't solve all the problem" you are assuming the rest of his code, which is the same thing I am doing, only in the opposite direction. – AlexT Jun 18 '18 at 16:09
  • The first condition would also need to return `amount.ToString();`. Otherwise, this sounds like what the OP was looking for to me. – Rufus L Jun 18 '18 at 16:13
  • @RufusL: the last condition as well - I already made that comment. – PaulF Jun 18 '18 at 16:19
  • See my comment to the question regarding accuracy of the return string - if full accuracy is expected the parameter needs to be decimal. – PaulF Jun 18 '18 at 16:20
0

It's correct, you are concatenate a string and your function returns float. Change the return method type.