This problem I have faced a lot of times, and always it differed somewhat from the previous times. So I created a function that can handle all.
Function String2Decimal(ptexString : Text[250];ptexFormat : Text[250];PtexDecimalSign : Text[1]) PdecReturnValue : Decimal
// String2Decimal
// Receives a string and a format and converts that to a decimal
// If a decimal sign is put into the string, then the rest of the size is not important,
// the right side is considered the decimal part, the left side is considered the integer part.
// The format is not considered in that case.
// Non-digit-characters are just filtered out ex: 'A5B6.D8R' => 56.8
// If no decimal sign is put into the string, the format is important and it is interpreted
// FROM RIGHT TO LEFT (and not from left to right)!!
// ex : '12345','IIIIIIDD' => 123.45
// ex2 : '12345','IDD' => 123.45 (the decimal digits are important, the rest is added when necessary)
// PARAMETERS :
// ptexString : the string to be converted
// ptexFormat : the format of the string (if it is a normal number, with a decimal sign if necessary,
// you can leave this parameter blank)
// i : integer part
// d : decimal part
// . : decimal sign
// PtexDecimalSign : ',' : decimal comma
// '.' : decimal dot
// other character : standard of Navision for decimal sign
// RETURN-VALUE : the decimal or 0 if something was wrong with ptexString
//
// EX: (the point is always the current decimal sign in these examples!!)
// '12345','II.DD' => 12.45
// '12345','IDD' => 123.45
// '12345','DD' => 123.45
// '12345','IIIIIIIDD',123.45
// '12345','' => 12345
// '-12345','' => -12345
// '12D3D4D5','' => 12345
// '12D3D4D5','I' => 12345
// '123.45','' => 123.45
// '123.45','III.DD' => 123.45
// '123.45','IID.DD' => 123.45
// '1,123.45','' => 1123.45
IF DELCHR(ptexFormat,'=','ID.') <> '' THEN
ERROR(STRSUBSTNO(ltxcFormatNotCorrect,ptexFormat));
// determine thousand and decimal seperator
IF (PtexDecimalSign <> ',') AND
(PtexDecimalSign <> '.') THEN
PtexDecimalSign := GetDecimalSign();
PdecReturnValue := 0;
lblnNegative := FALSE;
ltexIntegerPart := '';
ltexDecimalPart := '';
lint := NumEntries(ptexString,PtexDecimalSign);
IF lint = 1 THEN BEGIN
lintLen := STRLEN(ptexString);
lintLen2 := STRLEN(ptexFormat);
WHILE lintLen + lintLen2 > 0 DO BEGIN
IF lintLen2 = 0 THEN BEGIN
ltexIntegerPart := COPYSTR(ptexString,1,lintLen) + ltexIntegerPart;
lintLen := 0;
END
ELSE IF lintLen = 0 THEN
lintLen2 := 0
ELSE BEGIN
CASE FORMAT(ptexFormat[lintLen2]) OF
'D': ltexDecimalPart := COPYSTR(ptexString,lintLen,1) + ltexDecimalPart;
'I': ltexIntegerPart := COPYSTR(ptexString,lintLen,1) + ltexIntegerPart;
END;
END;
lintLen := lintLen - 1;
lintLen2 := lintLen2 - 1;
END;
END
ELSE BEGIN
ltexIntegerPart := Entry(1,ptexString,PtexDecimalSign);
ltexDecimalPart := Entry(2,ptexString,PtexDecimalSign);
END;
ltexIntegerPart := DELCHR(ltexIntegerPart,'=',DELCHR(ltexIntegerPart,'=','-+0123456789'));
ltexDecimalPart := DELCHR(ltexDecimalPart,'=',DELCHR(ltexDecimalPart,'=','-+0123456789'));
lintLen := STRLEN(ltexIntegerPart);
FOR lint := 1 TO lintLen DO BEGIN
CASE FORMAT(ltexIntegerPart[lint]) OF
'-': lblnNegative := TRUE;
'0'..'9': BEGIN
EVALUATE(lintHelp,FORMAT(ltexIntegerPart[lint]));
PdecReturnValue := PdecReturnValue * 10 + lintHelp;
END;
END;
END;
lintLen := STRLEN(ltexDecimalPart);
FOR lint := 1 TO lintLen DO BEGIN
CASE FORMAT(ltexDecimalPart[lint]) OF
'-': lblnNegative := TRUE;
'0'..'9': BEGIN
EVALUATE(lintHelp,FORMAT(ltexDecimalPart[lint]));
PdecReturnValue := PdecReturnValue + lintHelp * POWER(0.1,lint);
END;
END;
END;
IF lblnNegative THEN
PdecReturnValue := - PdecReturnValue;
Regards,Alain Krikilion No PM,please use the forum. || May the <SOLVED>-attribute be in your title!
Comments
tVar := FORMAT(1,376) //convert to text var
DELSTR(tVar, StrPos(tVar,','),1)
EVALUATE(dVar,tVar); //convert back to decimal
//Result should be 1376
Eric Wauters
MVP - Microsoft Dynamics NAV
My blog
It will create text string from the decimal without thousand separator (standard format 1)
MVP - Dynamics NAV
My BLOG
NAVERTICA a.s.
tvar :=FORMAT(Amount);
tvar :=DELSTR( tvar, STRPOS( tvar,','),1);
EVALUATE(Amount,tvar);
how do I pass Amount in new format to a codeunit??
I have the same problem with C/SIDE. I need to turn string to numerico and in the documentation I have not found any function to be able to make it.
Thanks beforehand.
A greeting, Juan.
No PM,please use the forum. || May the <SOLVED>-attribute be in your title!