Solved

Rounding Whole numbers

Posted on 2011-09-21
13
253 Views
Last Modified: 2012-05-12
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
Comment
Question by:MadIce
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
13 Comments
 
LVL 6

Expert Comment

by:psyclown-
ID: 36576799
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
 

Author Comment

by:MadIce
ID: 36576828
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
 
LVL 37

Expert Comment

by:Neil Russell
ID: 36576847
That dont help with the 3rd example.

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

I think?

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 37

Expert Comment

by:Neil Russell
ID: 36576979
Very quickly in excel so you can visualise.....

 Formula in excel
0
 
LVL 37

Expert Comment

by:Neil Russell
ID: 36577041
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
 

Author Comment

by:MadIce
ID: 36577113
Neilsr

I get an error for math.ceiling. doesn't except this many arguments
0
 

Author Comment

by:MadIce
ID: 36577222
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
 
LVL 37

Accepted Solution

by:
Neil Russell earned 500 total points
ID: 36577262
Ok so .net ceiling is different, ny bad....
Use

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

Me.thinks
0
 

Author Comment

by:MadIce
ID: 36577381
Neilsr
That's very close but can round down. I never want to round down.
0
 
LVL 37

Expert Comment

by:Neil Russell
ID: 36577520
Dim Answer = math.Round((Qty+ (5*10^LenFactor) -1 ) / (5*10^LenFactor)) * (5*10^LenFactor)

0
 
LVL 37

Expert Comment

by:Neil Russell
ID: 36577524
Hmmm no, Grrrrrr
Bloody .NET!
0
 
LVL 37

Expert Comment

by:Neil Russell
ID: 36577575
Or will it..... lol

Its 01:00 here and my eyes have gone! Good luck, bed time.
0
 

Author Closing Comment

by:MadIce
ID: 36581513
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question