Results 1 to 9 of 9

Thread: Converting to a number

  1. #1
    Join Date
    Oct 2007
    Location
    Denmark
    Posts
    409
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default Converting to a number

    Hi,

    I simple cannot figure how to convert Jagex's GE prices to a number:
    I would have a number like 146.0k and would like to convert it to 146000. How would i go around that?

  2. #2
    Join Date
    Feb 2007
    Posts
    3,616
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Pseudo code:

    if FindLetterK then
    begin
    int i;
    PutNumersIntoVariable(i);
    i := i * 1000;
    end;


    Like that? Just check if there is a K after the number, then get the numbers into a string and convert to integer, then multiply the integer number by 1000 and it should give you the real val. I don't know if you would use FindText or what, so you'd have to pick the function(s).

  3. #3
    Join Date
    Mar 2007
    Posts
    3,042
    Mentioned
    1 Post(s)
    Quoted
    14 Post(s)

    Default

    146.0 * k (1000)

    Get the letter after the last number (if there is one) and multiply by one, one thousand, or one million.
    :-)

  4. #4
    Join Date
    Oct 2007
    Location
    Denmark
    Posts
    409
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    SCAR Code:
    function ConvertNumber(N: String): Integer;
    begin
      if (Pos('k', N) <> 0) then
      begin
        N := TrimLetters(N);
        if (Pos('.', N) <> 0) then
          N := Replace(N, '.', ',');
        Result := StrToInt(N) * 1000;
      end;
      Writeln(Result);
    end;

    This is what I got? It fails at converting to an int, why?

  5. #5
    Join Date
    Apr 2007
    Location
    Perth, Australia
    Posts
    3,926
    Mentioned
    3 Post(s)
    Quoted
    2 Post(s)

    Default

    Quote Originally Posted by Frt View Post
    SCAR Code:
    function ConvertNumber(N: String): Integer;
    begin
      if (Pos('k', N) <> 0) then
      begin
        N := TrimLetters(N);
        if (Pos('.', N) <> 0) then
          N := Replace(N, '.', ',');
        Result := StrToInt(N) * 1000;
      end;
      Writeln(Result);
    end;

    This is what I got? It fails at converting to an int, why?
    Because you've got a ',' in there. It can't convert that into an integer. Instead of using TrimLetters(N) to remove the letters. Use GetNumbers(N) to get only the numbers.

  6. #6
    Join Date
    Jul 2007
    Location
    Right now? Chair.
    Posts
    8,488
    Mentioned
    3 Post(s)
    Quoted
    12 Post(s)

    Default

    Quote Originally Posted by Frt View Post
    SCAR Code:
    function ConvertNumber(N: String): Integer;
    begin
      if (Pos('k', N) <> 0) then
      begin
        N := TrimLetters(N);
        if (Pos('.', N) <> 0) then
          N := Replace(N, '.', ',');
        Result := StrToInt(N) * 1000;
      end;
      Writeln(Result);
    end;

    This is what I got? It fails at converting to an int, why?
    ints don't have ' , '

    edit:
    N := Replace(N, '.', '');

    ~RM

    I & I know Zion. It is in the spirit, body and mind of every one of us
    RMouse(obj: TMSIObject): boolean;

  7. #7
    Join Date
    Oct 2007
    Location
    Denmark
    Posts
    409
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Thanks guys, this is what I ended up with:
    SCAR Code:
    function ConvertNumber(N: String): Integer;
    begin
      if (Pos('k', N) <> 0) then
      begin
        N := Trim(Replace(N, 'k', ''));
        Result := Round(StrToFloat(N) * 1000.0);
      end;
      Writeln(Result);
    end;

  8. #8
    Join Date
    Jun 2007
    Location
    Wednesday
    Posts
    2,446
    Mentioned
    3 Post(s)
    Quoted
    1 Post(s)

    Default

    SCAR Code:
    function ConvertPrice(input: string): Integer;
    var
      f: Extended;
    begin
      input := Replace(Lowercase(Trim(input)), ',', '');
      if (Pos('m', input) > 0) then
        f := StrToFloatDef(Copy(input, 1, Pos('m', input) - 1), 0) * 1000000
      else if (Pos('k', input) > 0) then
        f := StrToFloatDef(Copy(input, 1, Pos('k', input) - 1), 0) * 1000
      else
        f := StrToFloatDef(Input, 0);
      Result := Round(f);
    end;
    Works for k and m. Your last snippet doesn't remove the comma's as well, so may be an issue.
    By reading this signature you agree that mixster is superior to you in each and every way except the bad ways but including the really bad ways.

  9. #9
    Join Date
    Oct 2007
    Location
    Denmark
    Posts
    409
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Quote Originally Posted by mixster View Post
    SCAR Code:
    function ConvertPrice(input: string): Integer;
    var
      f: Extended;
    begin
      input := Replace(Lowercase(Trim(input)), ',', '');
      if (Pos('m', input) > 0) then
        f := StrToFloatDef(Copy(input, 1, Pos('m', input) - 1), 0) * 1000000
      else if (Pos('k', input) > 0) then
        f := StrToFloatDef(Copy(input, 1, Pos('k', input) - 1), 0) * 1000
      else
        f := StrToFloatDef(Input, 0);
      Result := Round(f);
    end;
    Works for k and m. Your last snippet doesn't remove the comma's as well, so may be an issue.
    Oh yeah, forgot about mills, thanks!
    It's outputted as 75.5k so it can be turned into a float without any removals.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •