how do i make my code only display 1 or 2 numbers after the decimal?

[Solved Thank you!]

Here is the code that i have currently…

//This is in the update function

fishingLevelText.text = ("" + GameController.control.requieredFishingXP /GameController.control.currentFishingXP);

this works great, but it displays the percentage like … 5.987278123 % for example. I would like it to just say 5.9 % or 5.98 %

I thought i just had to add .toString(“F2”) but that wasnt seeming to work either.

edit: I had those in the wrong order for the division… i fixed that, and would like it to show .85 percent of the level or whatever thanks!

1 Like
String.Format("{0:0.00}", value);

Is how you format to 2 decimal places. In your case, I would also multiply value by 100 to get the actual percent:

fishingLevelText.text = ("" + GameController.control.requieredFishingXP /GameController.control.currentFishingXP);

var ctrl = GameController.control;
float value = (ctrl.requiredFishingXP / ctrl.currentFishingXP) * 100f;

fishingLevelText.text = String.Format("{0:0.00}", value) + " %";
3 Likes

could also just run

myInt.ToString("0.00");

on the int to convert it to a string representation of the int with only 2 decimal spaces.

8 Likes

@BenZed 's code: If you’re going to use string.Format, may as well avoid the extra concatenation. Also, use # instead of 0 for the first digit - it’ll include leading digits as needed, where 0 just does one digit IIRC.

fishingLevelText.text = string.Format("{0:#.00} %", value);

Finally, if ctrl.requiredFishingXP and ctrl.currentFishingXP are ints, then BenZed’s code will always show you 0% - it divides them as ints (resulting in 0) and only after that becomes a float when multiplied by 100f (but 0 * 100f = 0f). You need to cast at least one to a float before the operation:

float value = ( (float)ctrl.requiredFishingXP / ctrl.currentFishingXP) * 100f;
1 Like

ToString(“F2”) works fine.

–Eric

they are float values though not ints. i got it showing the correct percent now but i am still having issues working out how to get them to show only the percentage and not a bunch of decilmal places.

fishingLevelText.text = ("" +  GameController.control.currentFishingXP /GameController.control.requieredFishingXP * 100 + "%");

both variables are float values.

when i try that its saying i can do that to a string and float.

You need to use correct syntax. Since I don’t know what your code is, I can’t tell you what’s wrong.

–Eric

This is what i was trying.

fishingLevelText.text = ("" +  GameController.control.currentFishingXP /GameController.control.requieredFishingXP * 100 + "%" ).ToString("F2");

i also tried adding it to the end of each of the 2 variables instead and still got the error. i think i am just miss understanding where i am suppose to add it to.

thanks again for the help

You have to convert have to convert the numbers only, and then add other stuff like % symbols.

–Eric

I usually use :

Math.Round(number, 2, MidpointRounding.AwayFromZero).ToString("0.00");

This way it gets rounded to the second decimal but still represented as 2 decimals as a string.

oh cool thank you, i will see if i can get that way to work. so “number” would be the actual variable I am wanting to have rounded and displayed?

Thank you very much that worked beautifully!

I’m glad it worked for you!

1 Like