2

I'm trying to make a generic equation which converts a value. Here are some examples.

9,873,912 -> 9,900,000
125,930 -> 126,000
2,345 -> 2,400
280 -> 300
28 -> 30
In general, x -> n

Basically, I'm making a graph and I want to make values look nicer. If it's a 6 digit number or higher, there should be at least 3 zeros. If it's a 4 digit number or less, there should be at least 2 digit numbers, except if it's a 2 digit number, 1 zero is fine.

(Ignore the commas. They are just there to help read the examples). Anyways, I want to convert a value x to this new value n. What is an equation g(x) which spits out n?

It is for an objective-c program (iPhone app).

Sven Marnach
  • 574,206
  • 118
  • 941
  • 841
CodeGuy
  • 28,427
  • 76
  • 200
  • 317
  • I mean you have described everything, sounds like you can write that your self. – zellio Jun 28 '11 at 14:33
  • no, because I'm not sure how to do the rounding... – CodeGuy Jun 28 '11 at 14:34
  • Look up 'significant figures' (sometimes called 'significant digits'.) – Tim Jun 28 '11 at 14:38
  • 1
    "In general, x -> n" doesn't mean anything. Why is it there? – S.Lott Jun 28 '11 at 14:39
  • I'm showing you the variables that I'm trying to make my formula in terms of... – CodeGuy Jun 28 '11 at 14:41
  • Here are two very similar questions: [objective-c round number to nearest 50](http://stackoverflow.com/q/3285922/487781) and [Rounding numbers in Objective-C](http://stackoverflow.com/q/752817/487781). – hardmath Jun 28 '11 at 14:43
  • 1
    @reising1: "In general, x -> n" doesn't mean anything. It appears repeats the obvious. Why is it there? Simply repeating the obvious after the words "in general" doesn't seem to provide an information that's not already patently obvious. Yet, you included it. Why? Does it have something to do with the equally confusing "What is an equation g(x) which spits out n?"? Why all the extra words in there? – S.Lott Jun 28 '11 at 14:50

3 Answers3

6

Divide, truncate and multiply.

10**x * int(n / 10**(x-d))

What is "x"? In your examples it's about int(log10(n))-1.

What is "d"? That's the number of significant digits. 2 or 3.

S.Lott
  • 384,516
  • 81
  • 508
  • 779
1

Ahhh rounding is a bit awkward in programming in general. What I would suggest is dividing by the power of ten, int cast and multiplying back. Not remarkably efficient but it will work. There may be a library that can do this in Objective-C but that I do not know.

if ( x is > 99999 ) {
  x = ((int)x / 1000) * 1000;
}
else if ( x > 999 ) {
  x = ((int) x / 100) * 100;
}
else if ( x > 9 ) {
  x = ((int) x / 10) * 10;
} 
zellio
  • 31,308
  • 1
  • 42
  • 61
  • This works, but S. Lott's answer is first class. It is worth studying, especially the `int(log10(n)) - 1` piece, – Tim Jun 28 '11 at 15:44
  • Yeah, I'm with Tim Kemp on this. S.Lott has a more "correct" answer than I do. And understanding / utilizing it will be good overall. – zellio Jun 28 '11 at 15:46
0

Use standard C functions like round() or roundf()... try man round at a command line, there are several different options depending on the data type. You'll probably want to scale the values first by dividing by an appropriate number and then multiplying the result by the same number, something like:

int roundedValue = round(someNumber/scalingFactor) * scalingFactor;
Caleb
  • 124,013
  • 19
  • 183
  • 272