Page 1 of 1

Changing file size to decimal number

Posted: 2013-06-08 06:38:52
by Teebee
What am i doing wrong?

Code: Select all

program testscript;
uses
  StringUtils1;
  
var
  moviesize: string;
  moviesize2: integer;

  begin
  Moviesize := GetField(fieldsize);
  Showmessage(moviesize);
  
  moviesize2 := Format('Number           = %n', [moviesize]));
  showmessage(moviesize2)
  
 // ShowMessage(Format('Number           = %n', [12345.678]));
 
 
 
 end.

Posted: 2013-06-08 17:05:10
by antp
The size is a text string (containing numbers, but still text type).
So you should use a string variable and %s instead of %n.
Also, Format gives you a text, I am not sure why you try to put it in a Integer variable :??:
If you really need it as Integer, you can try StrToInt. If I remember well it works this way:
i := StrToInt(s, 0);
where i = integer variable, s = string variable to convert, 0 = the value in case it could not be converted.
If you need for a floating-point variable it would be StrToFloat (not sure if it exists in the script engine).

Posted: 2013-06-09 12:45:16
by Teebee
Only thing i really need is to convert that text string into a decimal number, so
12345678901 becomes 12.345.678.901

Code: Select all

program testscript;
uses
  StringUtils1;
  
var
  moviesize :  string;
  B: integer;

begin

  moviesize := getfield(fieldsize);
  showmessage(moviesize);
  B := StrToInt(moviesize,0);
  //  ShowMessage('number is '+IntToStr(moviesize,0));
  ShowMessage('B : '+IntToStr(B));
 end;
but B gives me 0 as a result instead of the correct value.

Posted: 2013-06-09 19:11:19
by antp
Integer are limited to 2147483647.
For larger values you will have to use float/double.
What is the final purpose? Adding dots between groups of 3 digits?

Posted: 2013-06-10 11:03:01
by Teebee
What is the final purpose? Adding dots between groups of 3 digits?
Correct.

Posted: 2013-06-10 11:18:10
by antp
Maybe it would be better to do that then, build a new text string by inserting a '.' every 3 numbers (starting at the end).
Or else with StrToFloat and Single or Double variable types.

Posted: 2013-06-16 15:21:12
by Teebee
I can't seem to get anywhere.

Is this real pascal, pascal innerfuse or deplhi programming? (can i find any documentation about it?)


i just want to convert the string to a decimal number.
but i only get identifier errors. Getting to the point of almost no return :/


Code: Select all

program testscript;
uses
  StringUtils1;
  
var
  moviesize,  MovieName : string;
  moviesize2 :  double;
   filelength , B , i , C : integer;
   begin
  moviesize := getfield(fieldsize);
  filelength :=length(moviesize);

 
ShowMessage('Filesize: "'+Moviesize+'" bytes');

 B := StrToInt(moviesize,0);
 // i := StrToFloat(moviesize,2);

      While (filelength > 1) Do
       begin
       Insert(',', moviesize, i);
       Dec(filelength, 3);
       showmessage(moviesize);
       end;
 end.
Any help is much appreciated :(

Posted: 2013-06-16 17:46:37
by antp
You were close.
It is a Innerfuse Pascal Script version 2, which is a scripting language that uses a simplified Pascal/Delphi syntax and some basic functions from it.
Some do not exist, for example "Dec" it seems.

Here is the working modified version:

Code: Select all

program testscript;

var
  moviesize: string;
  filelength: integer;

begin
  moviesize := getfield(fieldsize);
  filelength :=length(moviesize) - 2;

  ShowMessage('Filesize: "'+Moviesize+'" bytes');

  While (filelength > 1) Do
  begin
    Insert(',', moviesize, filelength);
    filelength := filelength - 3;
    showmessage(moviesize);
  end;
end.
You made an error in the Insert: you used "i" instead of your filelength variable.
Also, I initialized it at Length-2 since you have to insert the first separator two characters before the last character (i.e. 3 before the end).

I am not sure if the script engine includes formatting functions (as Format is not existing in it, it seems) so that way of inserting separators is probably easier than trying to format a number like what you were originally trying to do.
To get the size as single type (single = delphi's float type), there is
f := StrToFloat(moviesize);
but then you can't do much more with it...