- Home /
Floating Point and ToString issue
Hello guys,
I'm facing problem in finding the length a number .
float tempF = 123456789564353410.111213f;
string tempS = tempF.ToString();
Debug.Log(tempS.Length);
SO this basically prints 12 .
I want the length of the integer part only (no decimals). I cannot do this too
float tempF = 123456789564353410.111213f;
int tempI=(int)tempF;
since the number is exceeding the int range.
So How can i find the length of Integer part in Float.
float tempF = 123456789564353410.111213f;
Output i want is : 18
Answer by Noob_Vulcan · Dec 30, 2014 at 07:04 AM
Ok i found it ...
float tempF = 123456789564353410.111213f;
Math.Floor(Math.Log10(tempF)) + 1 ;
this will find the length
Math.Floor(Math.Log10(Your_Number)) + 1 **
If the floating point and precision isn't important, why not just use a string. If your float value has significance, you will lose it from go with numbers of that length. If you store as a string, well you can just lop off from the period to the length -1 with out all the math. Just curious.
@Landem : I tried with string .
float tempF = 123456789564353410.111213f;
string tempS = tempF.ToString();
Debug.Log(tempS.Length);
this will print 12. It will print right only if ur number has max 7 digits
No, i was asking from go why aren't you taking it in as a string if you're only(from what i can see/limited view based on questions) using it as a length qualifier. If you're doing any math on it, the precision is lost(if it mattered) and if it didn't, sweet, but it must since you want the length of the number. Again, just feeding my curiosity ;)
@Landem : hahaha... Actually m perfor$$anonymous$$g lots of calculations on the numbers(upto 38 digit numbers ).so I need float precision there.I needed length to show the digits in the form of million,trillion,octillion etc. rather than showing 10,000,000,000,000,000 . :D
so if i have 10,000,000.98383 i can show 10.000 million . Rest of the precision is taken care of at the backend .. :P
Answer by Landern · Dec 30, 2014 at 05:40 AM
first, use a decimal, it can hold more characters with percision then a float, plus decimal has a truncate method that removes the decimal and everything to the right.
decimal tempF = 123456789564353410.111213M;
int tempFLength = decimal.Truncate(tempF).ToString().Length;
This outputs 18 for me:
System.Console.WriteLine("tempF Value: " + tempF.ToString() + "\n" + "tempF Length " + tempFLength);
@Landem : Thanks bro ... this works fine ( even i may use Decimal ). But Decimal cannot hold more characters than Float (not taking about precision). Float stores upto 39 Digits whereas Decimal stores upto 29 digits.
So i was thinking is there a way to do the same for float(since i may need to store upto 39 digit).
Try this: $$anonymous$$ath.Truncate $$anonymous$$ethod (Double).
Yeah, I know it's for double but worth a shot.
with out using decimal you're going to lose information with float(single) and double(double precision float). You can see this when you use the ToString method and it returns scientific notation/exponential values. If you try and format it it(.ToString("F") or .ToString("F20") you will see the precision up to the the number 8 from the left... so... using a double would give you more, up to the 3 from the left, but then... waxy waxy goes the numbers. The storage of float(single) and doubles are to include the loss and expand on the exponent.