Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 933
  • Last Modified:

Round number to nearest tenth below original value.

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

0
Janrow
Asked:
Janrow
  • 4
  • 3
  • 3
2 Solutions
 
Jones911Commented:
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

0
 
JanrowAuthor 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

0
 
Jones911Commented:
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

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
PluckaCommented:
its easy

x = int(x/10) * 10
0
 
PluckaCommented:
In this example

 6.81.  I need 6.80.

x = int(x/.10) * .10
0
 
JanrowAuthor 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,
0
 
PluckaCommented:
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.
0
 
PluckaCommented:
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
0
 
JanrowAuthor 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.
0
 
Jones911Commented:
It's a fine solution plucka
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!

  • 4
  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now