Convert string in float ?

Just starting out? Need help? Post your questions and find answers here.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by ojahier.

Please Excuse my bad English.
I intente to convert the string :"0000567890123A" (and then" 00005678901231") in Float.
This is necessary for me for a profesional program who read a special file.
(The documentation indicate that the range of a float value is unlimited)

First, i posted this message in "BUG", I continue thinking it.

Exemple of my code :
-------------------
OpenConsole()

mt.s="0000567890123A"
If Mid(mt.s,14,1)="A"
s.s=Mid(mt.s,1,13)+"1"
r.f=ValF(s.s)
EndIf
-------------------

the resultat is : r.f = 5678901248.000000

what 's the problem?

Best regards

and


Commo estan? Bine ? Me alegro
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by tranquil.
Originally posted by ojahier

Please Excuse my bad English.
I intente to convert the string :"0000567890123A" (and then" 00005678901231" in Float with this code :
-------------------
OpenConsole()

mt.s="0000567890123A"
If Mid(mt.s,14,1)="A"
s.s=Mid(mt.s,1,13)+"1"
r.f=ValF(s.s)
EndIf
-------------------

the resultat is : r.f = 5678901248.000000
Hi, it seems that the value is out of range for floats. A value if 1678800000 gives right results.

Mike

Tranquilizer/ Secretly!
http://www.secretly.de
Registred PureBasic User
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by ojahier.

I need convert - of a file previously read - a string as "12345678901234" in numeric field for sum it.

Do you know if it's possible?

Commo estan? Bien ? Me alegro
(registered PureBasic user)
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by tranquil.

I do not needed such huge numbers yet but for such huge sums you may use doubles which are not included in PB yet. Many ppl asked for doublesupport but nothing happened yet!? Fred?

But you can take a look on Pauls great Ressource-Site where a codesnippset is postet to "emulate" doubles as long as they are standards in PB.

Cheers
Mike

Tranquilizer/ Secretly!
http://www.secretly.de
Registred PureBasic User
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by ojahier.

I 've get the codesnippset named "valf" (by paul) and it seems me that is the code added in PB.
I've tested the program (the codesnippset) and he gave me the same result that my program.

ex : number write = 12345678901234
and the result was : 12345679020032

Is there another way ?
IS it possible to use an external subroutine called with string, let it compute the val(string)? et return the sum of two number send as string to the subroutine?

Best regards


Commo estan? Bien ? Me alegro
(registered PureBasic user)
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Pupil.
Originally posted by ojahier

I 've get the codesnippset named "valf" (by paul) and it seems me that is the code added in PB.
I've tested the program (the codesnippset) and he gave me the same result that my program.

ex : number write = 12345678901234
and the result was : 12345679020032

Is there another way ?
IS it possible to use an external subroutine called with string, let it compute the val(string)? et return the sum of two number send as string to the subroutine?
I've created a procedure that adds two strings containing values and returns a string with the result, i don't know if this is exactly what your asking for but it's one way to go i think.. Anyway here's the code:

Code: Select all

Structure CharType
  Char.b
EndStructure

Procedure.s AddString(arg1.s, arg2.s)
DefType.CharType *p1, *p2, *pend1, *pend2
sum.s = "" : carry.l = 0
arg1 = "0" + arg1 : arg2 = "0" + arg2
a1 = FindString(arg1, ".", 1)
a2 = FindString(arg2, ".", 1)
If a1 Or a2
	If a1 : l1 = Len(arg1) - a1 : Else : arg1 + "." : EndIf
	If a2 : l2 = Len(arg2) - a2 : Else : arg2 + "." : EndIf
	If l1  l2
		If l1 > l2
			diff = l1 - l2
			sum = Right(arg1, diff)
			arg1 = Left(arg1, Len(arg1)-diff)
		ElseIf l2 > l1
			diff = l2 - l1
			sum = Right(arg2, diff)
			arg2 = Left(arg2, Len(arg2)-diff)
		EndIf
	EndIf
EndIf
	*pend1 = @arg1 : *p1 = *pend1 + Len(arg1) - 1
	*pend2 = @arg2 : *p2 = *pend2 +	Len(arg2) - 1
	Repeat
		If *p1\Char = '.'
			sum = "." + sum
		Else
			tmp = *p1\Char - '0' + *p2\Char - '0' + carry
			If tmp >= 10
				carry = 1
				tmp - 10
			Else
				carry = 0
			EndIf
			sum = Chr(tmp + '0') + sum
		EndIf
		If *p1 > *pend1 : *p1 - 1 : EndIf
		If *p2 > *pend2 : *p2 - 1 : EndIf
	Until *p1 = *pend1 And *p2=*pend2
	If carry
		sum = Chr(carry + '0') + sum
	EndIf
	ProcedureReturn sum
EndProcedure

; Example 1
b1.s = "1.123456789"
b2.s = "2.111111111"

sum.s = AddString(b1, b2)
Debug b1+" + "+b2+" = "+sum

; Example 2
b1.s = "999999999"
b2.s = "1"

sum.s = AddString(b1, b2)
Debug b1+" + "+b2+" = "+sum
Post Reply