|
|
|
#1 |
|
Master of disaster
Join Date: Feb 2004
Location: I'm here, why? You don't see me yet?
Posts: 333
|
Math.round() - Traitor
Hi for all readers,
I need to resolve next issue: For instance, if I have number with value 1545.445551122101, I would want to round up on only two decimals after point and all this divided in quarters i.e. 1234.13455… would be 1234.00 and 1234.45661… would be 1234.50. Additionally I would like to know how I could make to point out first group of No i.e. 1234.50 I would like to get 1.234.50. I hope that I was clear in my Explication, Thanks in advance |
|
|
|
|
|
#2 |
|
Senior Member
Join Date: Feb 2004
Location: Los Angeles
Posts: 2,920
|
Here's some code that will convert a number to the nearest quarter.
x = Math.round(x*4)/4; This will round-down to nearest quarter. x = Math.floor(x*4)/4; To add trailing zeros will require a bit more work: e.g. if (x - Math.floor(x) == .5) x += "0"; else if (x - Math.floor(x) == 0) x += ".00"; Sample usage:
I didn't understand the last part of your question (about inserting an extra period?) The method I used to add the trailing zeros is special cased for your problem. A more generic algorithm that adds trailing zeros would look like this: // x is the number you wish to add trailing zeros for var ix = Math.floor(x); var fx = x - ix; fx = String(fx*100+100).substr(1,2); x = ix + '.' + fx; trace(x); Last edited by jbum; 06-10-2004 at 07:42 PM. |
|
|
|
|
|
#3 |
|
Master of disaster
Join Date: Feb 2004
Location: I'm here, why? You don't see me yet?
Posts: 333
|
re
JBurn,
My friend if i may call you friend; you helped me so many times till now, but last one was grandiose (short and clear). Now a result that I get from the flash looks much better. You told me that you didn’t understand the last part of question, so I will try to explain better; note: I’m poor in Action Script, cause I depreciated it and spend a lot of time in learning ASP, SQL Server and VB. Whatever, I’m continuing: Macedonian money are denars that are very cheap regard to dollars or Euros, what means that has a lot of numbers, i.e. 1 dollar is equal to 55 denars. So I often get number larger then 100.000.00. So I would like to format numbers as follow: 12.045.50 or 1.450.50 instead 14500.50 or 1254.50. I hope that now I’m clearer and you could help me this time as well. Thanks in advance, Jugo |
|
|
|
|
|
#4 |
|
Senior Member
Join Date: Feb 2004
Location: Los Angeles
Posts: 2,920
|
Gotcha -- you're using the european convention where periods (.) are used to delineate thousands? I'm used to using commas:
100,000,000 dollars 100.000.000 denars In american currency, the period would only be used for fractional dollars (cents), for example, we might say: 100,000,000.25 to indicate a million dollars and twenty five cents. How would you represent this value? Would you write: 100.000.000 + 25/100 denars? or would you say 100.000.000.25 ? Or do you never have fractional denars? |
|
|
|
|
|
#5 |
|
Master of disaster
Join Date: Feb 2004
Location: I'm here, why? You don't see me yet?
Posts: 333
|
re
Jbum, friend,
I have to tell you about purpose of app, to preserve misunderstandings between us. I'm employed in the Bank on position Internal auditing. So I’m trying to make a useful app that would be used only from myself. Till now we did checking loans only by hands and nothing more, and it was takings me a lot of time. Now I hope that will cut the time for half at least. Means nothing that I would sell, so know that I very appreciate your help. I’m telling you this because you may see this like: - so I told him at least half code and he has whole app now. So don’t worry I’m really thankful person. One more time thanks Jburn |
|
|
|
|
|
#6 |
|
Master of disaster
Join Date: Feb 2004
Location: I'm here, why? You don't see me yet?
Posts: 333
|
re
Look Jbum,
Instead your cents we have deni it's the same 100th parts of denars as your cents are 100th parts of dollar. So i could use comma for denars as well. Only deni need to be separated with period. Thanks a lot |
|
|
|
|
|
#7 |
|
Senior Member
Join Date: Feb 2004
Location: Los Angeles
Posts: 2,920
|
Try this:
|
|
|
|
|
|
#8 |
|
Master of disaster
Join Date: Feb 2004
Location: I'm here, why? You don't see me yet?
Posts: 333
|
re
Wow,it works great,
It's too many for tonight for my heart .It works brilliant, but I don’t know to apply this function on all elements in array myData. Do I have to make another array ahead of x = RoundToQuarter(x); for instance: x = [z, k, a, c, b]; so after call the function for the whole array, please explain me this one yet. Thanks one more time Jbum, i appreciate it a lot, Jugo |
|
|
|
|
|
#9 |
|
Senior Member
Join Date: Feb 2004
Location: Los Angeles
Posts: 2,920
|
Would you explain why you're using an array? I don't really understand what your final app is supposed to look like or what it is supposed to do.
|
|
|
|
|
|
#10 |
|
Master of disaster
Join Date: Feb 2004
Location: I'm here, why? You don't see me yet?
Posts: 333
|
re
I thought that already sent you app, but never mind here is; I enclosed you whole app and AS. I forgot to tell you that denars must be increased if deni reach last Quarter, means 4th quarter and round up on .00 (then denars need to increase for 1).
i.e. 123,123,78925 needs to be 123,124.00 I hope that now you could find (from .FLA) the reason of applying EncodeCurrency, function to four values, and because of i need an array. Thanks a lot (it was for 100th time maybe ),Jugo |
|
|
|
|
|
#11 |
|
Master of disaster
Join Date: Feb 2004
Location: I'm here, why? You don't see me yet?
Posts: 333
|
re
Sorry jbum,
I took a look at your code below and i'm seeing the code that confute my last petition (round the number and increase last values). I hope the rest of question is still valid. |
|
|
|
|
|
#12 |
|
Master of disaster
Join Date: Feb 2004
Location: I'm here, why? You don't see me yet?
Posts: 333
|
To not confusing you additionally:
instead call function particulary for each member of my array that make table i want to call function to new array wich would consist all members. i.e. to preserve this:
i would like to build something like:
what you think about? |
|
|
|
|
|
#13 |
|
Master of disaster
Join Date: Feb 2004
Location: I'm here, why? You don't see me yet?
Posts: 333
|
re
It seems to me as I’m talking with my self only.
But never mind, relevant is only result that I’m getting from here, right? So Jbum, I could tell you that I found solution for all my dilemmas.
so only that is rest to me is figuring a dates of payment, means if I type some date into Release field i.e. 30.04.04 it need to continue providing next payment dates on every last day of the month. Regard to above typed date could be for next months: 31.05.04, 30.06.04 etc. but Also I don’t know what would happen if I need compute February cause it has 28 days and 29 in Leap Year. Do you have any idea about this? Thanks a lot Jugo |
|
|
|
|
|
#14 |
|
Senior Member
Join Date: Feb 2004
Location: Los Angeles
Posts: 2,920
|
Good morning
![]() Here's some sample code for working out a payment schedule.
Last edited by jbum; 06-11-2004 at 02:04 PM. |
|
|
|
|
|
#15 |
|
Master of disaster
Join Date: Feb 2004
Location: I'm here, why? You don't see me yet?
Posts: 333
|
re
Jbum,
I must tell you that you are flawless. Also I saw your web site and wow!!!.. (if all those things are your job you have spent lot of time in programming). Only code without stage using, I have no words for it. I didn’t know that AS is so powerful, I knew only for VB, C/C++ and JAVA, and fact that they are capable for such a things. Enough with Glorify, let’s back in real world, if you don’t mind. The script above is great but it’s workable for another purpose (maybe I’ll use it latter in my second project that I’ve been planning), but my present project want another approaching to the problem. I don’t need getData() object cause I will type the dates by hand, so regard to it dates will continue with further months, years and days considering the embedded dates by myself. I try to assuming something and I’m not sure to send whole code but I will send a little one that could be workable if someone guru use it properly.
I’m checking only short-term loans so I don’t need to know Leap Years before and after period of six maybe seven years. Thanks in advance, JUGO |
|
|
|
|
|
#16 |
|
Senior Member
Join Date: Feb 2004
Location: Los Angeles
Posts: 2,920
|
I'm not sure I understand, but I think you're saying you need to start with a user-input date, in the form: YY.MM.DD, which is stored in the string variable otplata.
You should replace the lines: var dat = new Date(); yr = dat.getFullYear(); // get current year mn = dat.getMonth(); // get current month (0-11) in my code above with this snippet: // read the date from a string datAry = otplata.split('.'); yr = datAry[0] + 2000; // full year mn = datAry[1] - 1; // month in 0-11 format Then it will compute the pay periods from that time on forward. Change the number 52 to the number of actual pay periods you want. Don't worry about your leap-year restriction - just use full years (2004) for the computation, and the code will work for quite a long time to come. |
|
|
|
|
|
#17 |
|
Master of disaster
Join Date: Feb 2004
Location: I'm here, why? You don't see me yet?
Posts: 333
|
re
It work fine and I’m sure that we would find very fast its defect. I tested all possible that I have knowledge about AS. GetMonth() works well except I would like to put zero ahead of number if is less than 10, but years don’t work naught as we expect; maybe reason is that I type date in format (this is like I see result in date field) 16.04.04, means I don’t embed whole year or just one number but for 2004 I use .04 and European format of date is:
DD/MM/YY so maybe it was one that confusing you. Thanks again |
|
|
|
|
|
#18 |
|
Senior Member
Join Date: Feb 2004
Location: Los Angeles
Posts: 2,920
|
Whoops - it was the DD.MM.YY thing. Change this part:
datAry = otplata.split('.'); yr = datAry[2] + 2000; // full year mn = datAry[1] - 1; // month in 0-11 format |
|
|
|
|
|
#19 |
|
Senior Member
Join Date: Feb 2004
Location: Los Angeles
Posts: 2,920
|
Whoops - it was the DD.MM.YY thing. Change this part:
datAry = otplata.split('.'); yr = datAry[2] + 2000; // full year mn = datAry[1] - 1; // month in 0-11 format or better yet, for the year part: yr = datAry[2]; if (yr < 100) yr += 2000; this will allow people to say both: 01.01.04 and 01.01.2004 |
|
|
|
|
|
#20 |
|
Master of disaster
Join Date: Feb 2004
Location: I'm here, why? You don't see me yet?
Posts: 333
|
re
I forgot to point out that result in my app in field datString is:31.11.112002
here is the whole script with last changing:
|
|
|
|
![]() |
|
||||||
| Thread Tools | |
| Display Modes | |
|
|