Bir String rakamı float'a döndürmek istiyorum. Nasıl Yapabilirm.
Örnek
Dim String1[11] as byte
Dim Flt1 as Float
String1="1.123"
Flt1=??? string1
Teşekkürler.
S.A.
Protonda kullanıyorum CCS de ama protonda bu oluyormu bilmiyorum ama ccs de oluyor.
CCS C için:
#INCLUDE <stdlib.h>
char string [10];
float x;
strcpy (string, "123.456");
x = atof(string);
// x is now 123.456
' Convert a real STRING of DECIMAL characters to an integer value
DEVICE = 18F452 ' Must be a 16-bit core device for Strings
DIM DEST_STRING as STRING * 10 ' Create a String capable of holding 10 characters
DIM WRD1 as WORD ' Create a variable to hold result
DEST_STRING = "1234" ' Load the STRING with DECIMAL digits
WRD1 = VAL(DEST_STRING,DEC) ' Convert the STRING into an integer
PRINT DEC WRD1 ' Display the integer as DECIMAL
STOP
Alıntı yapılan: muhittin_kaplan - 05 Ocak 2012, 19:56:42
' Convert a real STRING of DECIMAL characters to an integer value
DEVICE = 18F452 ' Must be a 16-bit core device for Strings
DIM DEST_STRING as STRING * 10 ' Create a String capable of holding 10 characters
DIM WRD1 as WORD ' Create a variable to hold result
DEST_STRING = "1234" ' Load the STRING with DECIMAL digits
WRD1 = VAL(DEST_STRING,DEC) ' Convert the STRING into an integer
PRINT DEC WRD1 ' Display the integer as DECIMAL
STOP
Hocam süpersiniz. Teşekkürler
bu işi dün bende araştırdım baya kasvetli iş
ama muhittin hocanın verdiği kod string to word ?
string değerimiz 1.234 olunca ne olacak ben onu bulamadım ?
işe yarar mı bilmiyorum ama belki bir çıkış noktası gösterir
uzun zamandır kullanmıyorum hatalar çıkabilir
Dim L As Byte
Dim P As Byte
Dim I As Byte
Dim FLT_STR[10] As Byte
Dim Flt As Float
L = Len FLT_STR
For I=0 To L
If FLT_STR[I] = "." Then
P = I
Break
EndIf
Next
For I=P To L-1
FLT_STR[I]=FLT_STR[I + 1]
Next
FLT_STR[L]=0
Flt = (1.0 * Val(FLT_STR,Dec))/(Pow 10, (L - P ))
Hepinize teşekkür ederim.
Aşağıdaki Kodla çözdüm.
'String to Float Variables
Dim N As Byte
Dim STF_STRING_IN[20] As Byte 'String to Float Variables
Dim STF_POWER_10 As Float
Dim STF_RESULT As Float
Dim STF_SIGN As Byte
Dim STF_CHAR As Byte
Dim STF_CHARPOS As Byte
Dim STF_TEMP As Float
Dim TEST_CHAR As Byte
Dim TEST_CHAR_IDX As Byte
Dim As String1 As String * 20
Dim FLT1 As Float
String1="4.321"
Ana:
Str STF_STRING_IN = Str String1
GoSub STRING_TO_FLOAT
FLT1 = STF_RESULT
Print at 1,1, Dec FLT1
End
STRING_TO_FLOAT:
STF_POWER_10 = 1
STF_RESULT = 0
STF_SIGN = 0
STF_CHARPOS = 0
STF_CHAR = STF_STRING_IN[STF_CHARPOS] ' \ Get a character from the String
Inc STF_CHARPOS ' /
If STF_CHAR = "-" Then ' Have we found a "-" character ?
STF_CHAR = STF_STRING_IN[STF_CHARPOS] ' \ Yes. So Discard the "-" character
Inc STF_CHARPOS ' /
STF_SIGN = 1 ' Indicate that the value is negative
Else If STF_CHAR = "+" Then ' Have we found a "+" character ?
STF_CHAR = STF_STRING_IN[STF_CHARPOS] ' \ Yes. So Discard the "+" character
Inc STF_CHARPOS ' /
EndIf
While 1 = 1 ' Scan the digits before the decimal point (if included)
If STF_CHAR < "0" Then Break ' \
If STF_CHAR > "9" Then Break ' / Exit the loop if non numeric characters found
STF_RESULT = STF_RESULT * 10 ' \
STF_RESULT = STF_RESULT + STF_CHAR ' Calculate the whole part of the floating point value
STF_RESULT = STF_RESULT - "0" ' /
STF_CHAR = STF_STRING_IN[STF_CHARPOS] ' \
Inc STF_CHARPOS ' / Get another character from the string
Wend
If STF_CHAR == "." Then ' Have we found a "." character ?
STF_CHAR = STF_STRING_IN[STF_CHARPOS] ' \ Yes. So Discard the "." character
Inc STF_CHARPOS ' /
While 1 = 1 ' Scan the digits after the decimal point (if included)
If STF_CHAR < "0" Then Break ' \
If STF_CHAR > "9" Then Break ' / Exit the loop if non numeric characters found
STF_POWER_10 = STF_POWER_10 * 10 ' \
STF_TEMP = (STF_CHAR - "0") / STF_POWER_10 ' Calculate the fractional part of the floating point value
STF_RESULT = STF_RESULT + STF_TEMP ' /
STF_CHAR = STF_STRING_IN[STF_CHARPOS] ' \
Inc STF_CHARPOS ' / Get another character from the string
Wend
EndIf
If STF_SIGN = 1 Then STF_RESULT = -STF_RESULT ' Convert to negative if required
Return