Precision / Accuracy Of Float Output

Hello,

I want to write positions of vertex-coordinates into a simple text file. This works fine like this:

CODE
float $posW[3] = pointPosition -w $comp;
fprint $fileId ("\t" + $posW[0] + " " + $posW[1] + " " + $posW[2] + "\n");

What I want to adjust is the precision of the float that is written. Like this i get 8 digits after the ".". Is there anything like setprecision from C++ to limit the digits?

Cheers.

QUOTE(gentlesea @ 01/09/09, 06:25 PM) [snapback]298563[/snapback]
What I want to adjust is the precision of the float that is written. Like this i get 8 digits after the ".". Is there anything like setprecision from C++ to limit the digits?
Cheers.

Not really, you need to do the round manually. Its not evry hard to do tough.

here's a sample:

CODE
//float to string with Tolerance Truncating
//dont ask for more than 13-15 digits
global proc string jooF2STT(float $float,int $tol){
  $b=(int)$float;
  string $ret = $b;
  if ($tol>0)
    $ret=$ret+".";
  $c=$float;
  for($i=0;$i<$tol;$i++){
    $c=(($c-(int)$c)*10);
    $ret=$ret+((int)$c);
  }
  return $ret;
}

//test
for($i=0;$i<15;$i++)
  print (jooF2STT(1.23456789012345678901234567,$i)+"\n");

If you want it to round add a 10^-$tol*0.5 to the float before this process ala:

CODE
//float to string with Tolerance Rounding
global proc string jooF2STR(float $float,int $tol){
  return jooF2STT($float+pow(10,-1*$tol)*0.5,$tol);
}

Thank you very much for the answer. There is one little glitch in your solution. If the value is a negative 0 with fraction, e.g. -0.1234 then your routing loses the - with the 0 when casting to int. Also $ret = $ret + (abs((int) $c)); was adjusted, because otherwise a lot of - are copied when using negative floats.

CODE
global proc string getStringOfFloatWithCertainToleranceDigits(float $float, int $tol)
{
    $b = (int) $float; // does not save the minus when being 0
    string $ret;
    if ($b == 0)
    {
        if ($float < 0)
        {
            $ret = "-" + $b;
        } else
        {
            $ret = $b;
        }
    } else
    {
        $ret = $b;
    }
    if ($tol > 0) $ret = $ret + ".";
    $c = $float;
    for ($i = 0; $i < $tol; $i++)
    {
        $c = (($c - (int) $c) * 10);
        $ret = $ret + (abs((int) $c));
    }
    return $ret;
}

Works for me like above now.

Oh i missed that, yes your right.