Page 1 of 1

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

Posted: Thu May 28, 2015 3:47 pm
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.

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

Posted: Thu May 28, 2015 3:55 pm
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)

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

Posted: Thu May 28, 2015 4:27 pm
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.

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

Posted: Thu May 28, 2015 4:31 pm
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

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

Posted: Thu May 28, 2015 4:37 pm
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:

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

Posted: Thu May 28, 2015 4:55 pm
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

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

Posted: Fri May 29, 2015 2:17 am
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