• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 268
  • Last Modified:

Rounding Whole numbers

I'm having a hard time rounding. I pull a quantity from a stored procedure and need to round this number up. It will be used as the MaximumScale number for a Excel chart. I never know legth (number of digits it will be) examples:

2300 needs to be 2500
2500 needs to be 3000
234000 needs to be 250000 and so forth.
Been trying to use Math.Ceiling and Math.Floor.

Math.Ceiling(Qty / Num) * num
But not sure how to determine num or is there a better way.
0
MadIce
Asked:
MadIce
  • 7
  • 5
1 Solution
 
psyclown-Commented:
Hello,

Math.Round(Qty / num) * num could work for you, where num is the "rounder".

ex.

double qty = 2300;
double num = 500;
double result = Math.Round(qty / num) * num;

that would give you 2500.

I hope it is useful.

Best Regards,
Jesper
0
 
MadIceAuthor Commented:
That would probably work if I can determine what num should be. num could be 5, 50, 500, 5000, 50000 and so forth. How can I determine that number?
0
 
Neil RussellTechnical Development LeadCommented:
That dont help with the 3rd example.

num needs to be 5 * 10^( the length of qty in digits -2)

I think?

0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Neil RussellTechnical Development LeadCommented:
Very quickly in excel so you can visualise.....

 Formula in excel
0
 
Neil RussellTechnical Development LeadCommented:
Sooooo.....

Dim Qty as Double = 243000D
Dim StrQty as String = Qty.ToString("D")
Dim LenFactor as Integer = StrQty.Length() - 2

Dim Answer = math.ceiling(Qty+1,5*10^LenFactor)

Think that works but not on a machine with .net on right now.

Open in new window

0
 
MadIceAuthor Commented:
Neilsr

I get an error for math.ceiling. doesn't except this many arguments
0
 
MadIceAuthor Commented:
OK, got part of it.

Dim Qty as int32 = spReturnNum   'Say it = 27105
dim num as int32 = (Math.Log10(Qty)) - 1
dim qt as int32 = Math.Ceiling(Qty) + (5 * 10 ^ num)

This gives me 32105 but what I really need is 32000.
The (5 * 10 ^ num) part has helped.
0
 
Neil RussellTechnical Development LeadCommented:
Ok so .net ceiling is different, ny bad....
Use

Dim Answer = math.Round((Qty+1)/(5*10^LenFactor)) * (5*10^LenFactor)

Me.thinks
0
 
MadIceAuthor Commented:
Neilsr
That's very close but can round down. I never want to round down.
0
 
Neil RussellTechnical Development LeadCommented:
Dim Answer = math.Round((Qty+ (5*10^LenFactor) -1 ) / (5*10^LenFactor)) * (5*10^LenFactor)

0
 
Neil RussellTechnical Development LeadCommented:
Hmmm no, Grrrrrr
Bloody .NET!
0
 
Neil RussellTechnical Development LeadCommented:
Or will it..... lol

Its 01:00 here and my eyes have gone! Good luck, bed time.
0
 
MadIceAuthor Commented:
I believe what I have below is working. The (5 * 10 ^ num) part really helped
Thanks alot.

Dim Qty As Int32 = SPGetQty
Dim Num as Int32 = (Math.Log10(Qty)) - 1
Dim answer As Int32 = (Math.Round((Qty) / (5 * 10 ^ num)) * (5 * 10 ^ num))

If Qty >= answer Then
    answer += 5 * 10 ^ num
End If
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 7
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now