- Home /
2 decimal wont coperate
string RounderA = RaycastHit.distance.ToString();
RounderA = string.Format("{0:0.##}", RounderA);
tmA.text = RounderA;
Ignores my string.format
i want to set raycasthit.distance to show only 2 decimal 0.02 isted he is showing 0.023345354
Answer by Bunny83 · Oct 15, 2014 at 06:03 PM
The problem is you don't feed the float value into string.Format. You pass it already as string and a string isn't formatted but simply copied to the result.
either use:
string RounderA = string.Format("{0:0.##}", RaycastHit.distance);
tmA.text = RounderA;
or
string RounderA = RaycastHit.distance.ToString("0.00");
tmA.text = RounderA;
if i try any of thos too i get ReycastingSuspension.cs(42,78): error CS0120: An object reference is required to access non-static member `UnityEngine.RaycastHit.distance'
RaycastHit is the type of return, you should have a variable say called hit ins$$anonymous$$d.. Then it would be hit.distance.
@isometricLove: :D I almost expected that result... I just copied your own code example... ^^
It's perfectly possible to use "RaycastHit" as variable name however it wouldn't make any sense and would be horrible style to do that since you're hiding the RaycastHit type inside your class.
i did this
var hitdisA = hitColectorPointA.distance;
string RounderA = string.Format("{0:0.##}", hitdisA);
tmA.text = RounderA;
duno if its bad but it does the job
Your answer
Follow this Question
Related Questions
decimal to fraction (just display) 1 Answer
Save decimals in PlayerPrefs c# 2 Answers
reducing a percentage value to desired decimal places 4 Answers