Author Topic: Rounding floating values  (Read 810 times)

0 Members and 1 Guest are viewing this topic.

DarkCrusade

  • Guest
Rounding floating values
« on: July 14, 2013, 04:45:17 am »
Hello, I am encountering a problem rounding floats with Soldat's scriptcore due to no implementation of the powerfunction. I tried to write my own, but apparently failed..

Code: (pascal) [Select]
function power(x,y:integer):single;
begin
 if y = 1
  then result := x
  else result := x * power(x,y-1);
end;

function round2(const num:single; const places:longint):single;
var t:single;
begin
 t := power(10,places);
 result := round(num*t)/t;
end;

procedure test();
var x:single;
begin
 x := 3.5000000000000000;
 x := round2(x,4);
 writeln(floattostr(x)); // Outputs 3.5000000000
end;

Could anyone help me here? Where do I go wrong?

Offline Furai

  • Administrator
  • Veteran
  • *****
  • Posts: 1908
    • TransHuman Design
Re: Rounding floating values
« Reply #1 on: July 14, 2013, 06:57:49 am »
Are you just trying to round it up/down? Or what exactly?
"My senses are so powerful that I can hear the blood pumping through your veins."

DarkCrusade

  • Guest
Re: Rounding floating values
« Reply #2 on: July 14, 2013, 07:27:15 am »
I want all numbers to be rounded to two decimal places. All the floating values I use use no more than two decimal places, and having console outputs of 20 iterations of '0' is simply ugly.

Offline chutem

  • Veteran
  • *****
  • Posts: 1119
Re: Rounding floating values
« Reply #3 on: July 14, 2013, 07:32:27 am »
When you mean round, are you trying to display the output of 3.5 rounded to 4 dp as 3.5000 instead of printing out a bunch of extra zeros, or is there something else which is not working? You weren't very clear.

If you are trying to print fewer zeros, you will have to manipulate the string, what you are doing in your code will have no effect on the number that is stored if the number has the same value rounded or not
1NK3FbdNtH6jNH4dc1fzuvd4ruVdMQABvs

Offline Falcon`

  • Flagrunner
  • ****
  • Posts: 792
  • A wanted lagger
Re: Rounding floating values
« Reply #4 on: July 14, 2013, 07:51:35 am »
If you're not paying for something, you're not the customer; you're the product being sold.
- Andrew Lewis

Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.

DarkCrusade

  • Guest
Re: Rounding floating values
« Reply #5 on: July 14, 2013, 10:10:49 am »
@chutem: Yes, I want the extra zeros removed. My English fails me sometimes. The problem is that I receive a string value (take 3.5 for an example) which I want to convert to a single. The function StrToFloat will store the extra zeros as well, though, and I see no reason why I should iterate through the complete string until I find the part where only zeros are stored.

My new approach was this:

Code: (pascal) [Select]
function floattostr2(const val:single):string;
var i,j,lastnum:byte;
begin
 result := floattostr(roundto(val,2));
 
 for i := 1 to length(result) do
  if copy(result,i,i) = '.' then begin
   for j := i+1 to length(result) do
    if copy(result,j,j) <> '0' then lastnum := j;
   result := copy(result,1,lastnum);
  end;
end;

But it failed as well.
« Last Edit: July 14, 2013, 10:20:09 am by DarkCrusade »

Offline Rzaba

  • Major(1)
  • Posts: 6
Re: Rounding floating values
« Reply #6 on: July 14, 2013, 03:34:23 pm »
Hello, I have just written this simple function, but I'm not sure this is exactly what you want to achieve. This function truncates a float to two decimal digits and returns that value as a string.

Code: [Select]
function FloatToStr2(val: single):string;
var
i: integer;
temp_str: string;
begin
temp_str := FloatToStr(val); //get the float with all zeros

//find the dot
for i := 1 to length(temp_str) do begin
if temp_str[i] = '.' then begin
result := copy(temp_str, 1, i+2);
break;
end;
end;
end;

Hope this helps!
Tools: LSA

DarkCrusade

  • Guest
Re: Rounding floating values
« Reply #7 on: July 21, 2013, 07:52:59 pm »
Works. I am *headdesking* because I did not think of that.. ;)