We help IT Professionals succeed at work.

Round number to nearest tenth below original value.

Medium Priority
1,005 Views
Last Modified: 2013-12-24
I'm converting byte size to cents. 1MB should equal about 1 cent. I want to set a variable to the nearest tenth of a dollar below its current value.  If the original number is 682, I want the reslutant number to be 680. If the number is 689, I want it to be 680, not 690.

Accordingly, if the number is 10263 or 10268, I want it to be 10260.

I tried rounding the number (round) but this doesn't quite work, goes to the nearest number, above or below. I always want below.

Cost is derived from the total file byte size and will be converted to a dollar format so that 680 will be $6.80. I don't want the customer to be overcharged, even if it's such a small fraction. And I don't much care for cent amounts that are too diffucult to add for the customer. Tenths of a dollor works for me. That's what I want.
<CFSET cost = NumberFormat(byte_count/1024/1024/100, "9.99")>

Open in new window

Comment
Watch Question

Commented:
Try this.
<cfset my_number1 = 682 />
<cfset my_number2 = 689 />
<cfset my_number3 = 10263 />
<cfset my_number4 = 10268 />
 
<cfoutput>#roundDownByTen(my_number1)# <br /></cfoutput>
<cfoutput>#roundDownByTen(my_number2)# <br /></cfoutput>
<cfoutput>#roundDownByTen(my_number3)# <br /></cfoutput>
<cfoutput>#roundDownByTen(my_number4)# <br /></cfoutput>
 
<cffunction name="roundDownByTen" returntype="numeric">
	<cfargument name="someNumber" required="true">
    <cfreturn arguments.someNumber - (arguments.someNumber MOD 10) />
</cffunction>

Open in new window

Author

Commented:
That didn't quite get it, but I'm close.

Not sure why you're repeating those numbers, my_number1, my_number2, etc. I substituted what I think was your idea with my code below and got this:

684.917331696
681

Okay, that helps but after I reduce it I'm still left with 6.81.  I need 6.80.

Not sure at all what your cffunction does, or how I use it. But I put it in place, but it comes after your roundDownByTen tag (which I did not know existed). Maybe you can explain what you're doing so I can extrapolate.
<cfset cost = byte_count/1024/1024>
<cfoutput>#cost# <br /></cfoutput>
<cfoutput>#round(roundDownByTen(cost))# <br /></cfoutput>

Open in new window

Commented:
Hmm if you look I am giving the 4 examples you specified.

<cfset my_number1 = 684.917331696 />
<cfoutput>#roundDownByTen(my_number1)# <br /></cfoutput>
 
<cffunction name="roundDownByTen" returntype="numeric">
	<cfargument name="someNumber" required="true">
    <cfset arguments.someNumber = round(arguments.someNumber) />
    <cfreturn arguments.someNumber - (arguments.someNumber MOD 10) />
</cffunction>

Open in new window

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Commented:
its easy

x = int(x/10) * 10

Commented:
In this example

 6.81.  I need 6.80.

x = int(x/.10) * .10

Author

Commented:
Plucka: Looks like Jones911 should have this, but that looks interesting enough that if it works I'll give you a few points if you can tell me what X is and what int represents.

Seems to me X is the unknown, in this case, the byte count, and int would probably be an interger, and if so, which, what, and how many?

I have the byte_count, so, just for the sake of the math, would you substitute the x and the int for values I can use, and I'll give it a try.

I'll wait another ten minutes.  Thanks,

Commented:
x is your variable with the number

ie

<cfset x = 682 />
<cfset x = int(x/10) * 10 />

<cfoutput>#x#</cfoutput>

is 680, much simpler solution.
Commented:
int is get the interger value of a number, ie int(1.52) = 1

so 682 / 10

= 68.2
int (68.2) = 68
68 * 10 = 680

this

int(variableName / 10) * 10

will give you your result every time

Author

Commented:
I have to agree with you Plucka. That's pretty nifty and lean. Seems I remember some kind of similar math back in high school. Or was it grade school.

So, Jones911 was first but your solution was leaner so I hope you all agree we split the points 300/200 in your favor. Better is superior to first. And you're both superior experts.

Commented:
It's a fine solution plucka
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.