Posted By: ploe | May 16th, 2008 @ 10:40 AM
page 1 of 1
Comments: 6 | Views: 895

I've been thinking about this problem for a while and don't know how to solve it without using strings...

The method looks like this...
double Format(double d, double precision)
{
   ...
}

d is the double I want to format and precision is how it needs to be formatted

for example:
d = 2.06
precision = 1.0
returns: 2

d = 36.04
precision = 1.0
returns: 6

d = 36.04894
precision = 2.3
returns: 36.048

It's only a few lines of code if I convert the two parameters to strings, but I feel like there must be a mathematical approach to solve this problem too...any ideas? Expressionless

TommyCarlier
TommyCarlier
I want my scalps!
Here's something I wrote years ago that does something like that: http://tommycarlier.blogspot.com/2004/10/rounding-floating-point-value-to.html
Why not use the Mod operator (% in C#) and some loops?
evildictaitor
evildictaitor
if( !succeed( try() ) ) { while(true) try(); }
Am I the only person who sees practically no relation between your examples?

If you want to round A to the nearest B:

double RoundToTheNearest(double A, double B){
  return A * (int)( (A + B/2) / B);
}

or if you want to get the number of units of precision:

public static double Truncate(double num, int digits)
{
    if (digits < 1)
        throw new ArgumentOutOfRangeException("digits", digits, "number of digits must be >= 1");

    double f;

    if (Math.Abs(num) > 1)
    {
        int p;
        p = (int)Math.Ceiling(Math.Log10(num));
        f = Math.Pow(10, digits - p);
    }
    else
        f = Math.Pow(10, digits);
           
    return num - (num % f);
}

I think I understand the problem but it isn't what I know by precision. If you supply a prevision paramter of X.Y, you want:

You want:
      First (X) rightmost digits before the deciaml point
      First (Y) leftmost digits after the decimal point

So, supplied with a double of format ABCD.EFGH and a precision of 3.4

you would return BCD.EFGH

ploe wrote:

precision = 1.0


You want:
      First (1) rightmost digits before the deciaml point
      First (0) leftmost digits after the decimal point

ploe wrote:

precision = 2.3


You want:
      First (2) rightmost digits before the deciaml point
      First (3) leftmost digits after the decimal point

Is this what you want, or have I misinterpreted this completely?

Kevin
evildictaitor
evildictaitor
if( !succeed( try() ) ) { while(true) try(); }
Ah! Ok. it's all easy when you put it like that:

double FunnyPrecisionThing(double f, int left, int right){
  double leftPart = f % Math.Pow(10, left);
  double rightPart = (f - (int)f) % Math.Pow(10, right);
  return leftPart + rightPart;
}

if you want to keep your weird A.B precision notation:

double FunnyPrecisionThing(double f, double p){
  double basePart = p - (int)p;
  while(basePart - (int)basePart > double.epsilon) basePart *= 10;
  return FunnyPrecisionThing(f, (int)p, (int)basePart);
}

Note that by using the A.B notation you can potentially suffer from floating point problems.
page 1 of 1
Comments: 6 | Views: 895
Microsoft Communities