quick question: how to set the length of a float variable

Just starting out? Need help? Post your questions and find answers here.
mao
User
User
Posts: 20
Joined: Wed May 27, 2015 2:27 am

quick question: how to set the length of a float variable

Post by mao »

how to set the length of a float variable?
I try to save a string(with a numeric value) into a float type, but have no idea how to set the limitation of the length of float variable.
for example:
string.s = "0.99999"
debug ValF(string)

which returns 32bit, but I only need 4 or 5 digits after the decimal point. How to get that? Thanks.
Julian
Enthusiast
Enthusiast
Posts: 276
Joined: Tue May 24, 2011 1:36 pm

Re: quick question: how to set the length of a float variabl

Post by Julian »

I assume that you want to cut it to 4-5 decimal places when you display it to the user?

If so, you can use:

Code: Select all

Result$ = StrF(Value.f [, NbDecimal])
e.g.:

Code: Select all

string.s = "0.99999"
Debug ValF(string)
Debug StrF(ValF(string) ,5)
mao
User
User
Posts: 20
Joined: Wed May 27, 2015 2:27 am

Re: quick question: how to set the length of a float variabl

Post by mao »

Julian wrote:I assume that you want to cut it to 4-5 decimal places when you display it to the user?

If so, you can use:

Code: Select all

Result$ = StrF(Value.f [, NbDecimal])
e.g.:

Code: Select all

string.s = "0.99999"
Debug ValF(string)
Debug StrF(ValF(string) ,5)

Hi Julian! StrF does cut the string to specific length, but now we get string again rather than numeric value.
Julian
Enthusiast
Enthusiast
Posts: 276
Joined: Tue May 24, 2011 1:36 pm

Re: quick question: how to set the length of a float variabl

Post by Julian »

mao wrote:Hi Julian! StrF does cut the string to specific length, but now we get string again rather than numeric value.

Code: Select all

testString.s = "0.99999"
testNumber.f = ValF(testString)
Debug testNumber

;do some float maths here

newString.s = StrF(testNumber, 5)
Debug newString
Marc56us
Addict
Addict
Posts: 1600
Joined: Sat Feb 08, 2014 3:26 pm

Re: quick question: how to set the length of a float variabl

Post by Marc56us »

You can't store float with a specific number of decimal digits

Look PB help (Variables and Types Chapter)
Special information about Floats and Doubles

A floating-point number is stored in a way that makes the binary point "float" around the number, so that it is possible to store very large numbers or very small numbers. However, you cannot store very large numbers with very high accuracy (big and small numbers at the same time, so to speak).

Another limitation of floating-point numbers is that they still work in binary, so they can only store numbers exactly which can be made up of multiples and divisions of 2. This is especially important to realize when you try to print a floating-point number in a human readable form (or when performing operations on that float) - storing numbers like 0.5 or 0.125 is easy because they are divisions of 2. Storing numbers such as 0.11 are more difficult and may be stored as a number such as 0.10999999. You can try to display to only a limited range of digits, but do not be surprised if the number displays different from what you would expect!


This applies to floating-point numbers in general, not just those in PureBasic.
[...]

http://www.purebasic.com/documentation/ ... ables.html

This is why some software store smalls numeric values as string and convert it as number only if calculation is need
:wink:
mao
User
User
Posts: 20
Joined: Wed May 27, 2015 2:27 am

Re: quick question: how to set the length of a float variabl

Post by mao »

Marc56us wrote:You can't store float with a specific number of decimal digits


This is why some software store smalls numeric values as string and convert it as number only if calculation is need
:wink:

Thanks! I may keep the full length of it
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: quick question: how to set the length of a float variabl

Post by IdeasVacuum »

..... PB supports doubles too: Result.s = StrD(Value.d [, NbDecimal])

Code: Select all

MyDouble.d = 123.123456789
MyString.s = StrD(MyDouble, 4)
Debug MyString ;123.1235
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
Post Reply