To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here


A Flash Developer Resource Site

Go Back   Flash Kit Community Forums > Flash Help > Flash ActionScript

Reply
 
Thread Tools Display Modes
Old 06-10-2004, 07:17 PM   #1
jugomkd75
Master of disaster
 
jugomkd75's Avatar
 
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
__________________

there is no handcraft without tool!
www.jugoars.com
jugomkd75 is offline   Reply With Quote
Old 06-10-2004, 07:22 PM   #2
jbum
Senior Member
 
jbum's Avatar
 
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:

code:

// make a random number & show it
x = Math.random()*100+Math.random();
trace(x);

// Round to nearest quarter and show it
x = Math.round(x*4)/4;
if (x - Math.floor(x) == .5) x += "0";
else if (x - Math.floor(x) == 0) x += ".00";
trace(x);



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.
jbum is offline   Reply With Quote
Old 06-10-2004, 08:01 PM   #3
jugomkd75
Master of disaster
 
jugomkd75's Avatar
 
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
__________________

there is no handcraft without tool!
www.jugoars.com
jugomkd75 is offline   Reply With Quote
Old 06-10-2004, 08:08 PM   #4
jbum
Senior Member
 
jbum's Avatar
 
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?
__________________
jbum is offline   Reply With Quote
Old 06-10-2004, 08:16 PM   #5
jugomkd75
Master of disaster
 
jugomkd75's Avatar
 
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
__________________

there is no handcraft without tool!
www.jugoars.com
jugomkd75 is offline   Reply With Quote
Old 06-10-2004, 08:22 PM   #6
jugomkd75
Master of disaster
 
jugomkd75's Avatar
 
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
__________________

there is no handcraft without tool!
www.jugoars.com
jugomkd75 is offline   Reply With Quote
Old 06-10-2004, 08:31 PM   #7
jbum
Senior Member
 
jbum's Avatar
 
Join Date: Feb 2004
Location: Los Angeles
Posts: 2,920
Try this:

code:

kDelimiter = '.'; // switch to comma for american style

RoundToQuarter = function(x)
{
return Math.round(x*4)/4;
}

EncodeCurrency = function(x)
{
var ix = Math.floor(x);
var fx = x - ix;
fx = String(fx*100+100).substr(1);

if (ix >= 1000) {
ix = String(ix);
var len = ix.length;
var ngroups = 1 + Math.floor((len-4)/3);
var ox = '';
while (ngroups--)
{
ox = kDelimiter + ix.substr(len-3,3) + ox;
ix = ix.substr(0,len-3);
len -= 3;
}
ix += ox;
}
return ix + '.' + fx;
}

x = Math.random()*10000000+Math.random();
trace(x);

x = RoundToQuarter(x);
trace('nearest quarter:' + x);

trace('denars: ' + EncodeCurrency(x));


__________________
jbum is offline   Reply With Quote
Old 06-10-2004, 09:08 PM   #8
jugomkd75
Master of disaster
 
jugomkd75's Avatar
 
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
__________________

there is no handcraft without tool!
www.jugoars.com
jugomkd75 is offline   Reply With Quote
Old 06-11-2004, 12:59 AM   #9
jbum
Senior Member
 
jbum's Avatar
 
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.
__________________
jbum is offline   Reply With Quote
Old 06-11-2004, 05:49 AM   #10
jugomkd75
Master of disaster
 
jugomkd75's Avatar
 
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
Attached Files
File Type: zip loan amortization.zip (168.8 KB, 807 views)
__________________

there is no handcraft without tool!
www.jugoars.com
jugomkd75 is offline   Reply With Quote
Old 06-11-2004, 07:00 AM   #11
jugomkd75
Master of disaster
 
jugomkd75's Avatar
 
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.
__________________

there is no handcraft without tool!
www.jugoars.com
jugomkd75 is offline   Reply With Quote
Old 06-11-2004, 07:15 AM   #12
jugomkd75
Master of disaster
 
jugomkd75's Avatar
 
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:
code:
q = Math.random()*10000000+Math.random();
trace(q);
q = RoundToQuarter(q);
b = Math.random()*10000000+Math.random();
trace(b);
b= RoundToQuarter(b);
a = Math.random()*10000000+Math.random();
trace(a);
a= RoundToQuarter(a);
trace('denars: ' + EncodeCurrency(q));
trace("b = "+ EncodeCurrency(b));
trace("a = "+ EncodeCurrency(a));


i would like to build something like:
code:

var k = new Array (a, x, b);
k = RoundToQuarter(k);
trace('denars: ' + EncodeCurrency(k));


what you think about?
__________________

there is no handcraft without tool!
www.jugoars.com
jugomkd75 is offline   Reply With Quote
Old 06-11-2004, 12:04 PM   #13
jugomkd75
Master of disaster
 
jugomkd75's Avatar
 
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.
code:

myData = [];
for (i=0; i < rati+1; i++){
m=i+1;
k=z*p/1200;
b = a-k;
//this is for separating integers with periods;
k= RoundToQuarter(k);
b= RoundToQuarter(b);
a= RoundToQuarter(a);
z= RoundToQuarter(z);

//this fulfill array;
myData[i] = [m, rati+"/02/04", EncodeCurrency(z), EncodeCurrency(b), EncodeCurrency(k), EncodeCurrency(a)];
z=z-b;

}


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
__________________

there is no handcraft without tool!
www.jugoars.com
jugomkd75 is offline   Reply With Quote
Old 06-11-2004, 02:02 PM   #14
jbum
Senior Member
 
jbum's Avatar
 
Join Date: Feb 2004
Location: Los Angeles
Posts: 2,920
Good morning

Here's some sample code for working out a payment schedule.

code:

// Utility function to determine if leap year
isLeapYear = function(year)
{
return ( year % 4 == 0 &&
(year % 100 != 0 || year % 400 == 0) );

}

// Utility function to determine number of days in month
// (including leap years)
kmDays = [31, 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31];
getMonthDays = function(year,mNbr) // month is from 0 - 11
{
return kmDays[mNbr] + (mNbr == 1 && isLeapYear(year));
}

// Sample Usage
// print end of month for next 52 pay periods

var dat = new Date();
yr = dat.getFullYear(); // get current year
mn = dat.getMonth(); // get current month (0-11)

// loop over number of pay periods
for (var i = 0; i < 52; ++i)
{
var dat = new Date(yr,mn,getMonthDays(yr,mn));
var datString = dat.getFullYear() + '.' + (dat.getMonth()+1) + '.' + dat.getDate();

trace(i + ": " + datString);

// increment month - if we reach end o f year, reset month and increment year
if (++mn >= 12) {
mn = 0;
yr++;
}
}


__________________

Last edited by jbum; 06-11-2004 at 02:04 PM.
jbum is offline   Reply With Quote
Old 06-11-2004, 06:19 PM   #15
jugomkd75
Master of disaster
 
jugomkd75's Avatar
 
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.
code:

//otplata is an input Text where I’m typing date in format 30.05.04 where last group (04) present loan disbursement year;
If (otplata.substring ( 6, 7 ) eq 96&&00&&04&&08){
February = 29;
}


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
__________________

there is no handcraft without tool!
www.jugoars.com
jugomkd75 is offline   Reply With Quote
Old 06-11-2004, 06:30 PM   #16
jbum
Senior Member
 
jbum's Avatar
 
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.
__________________
jbum is offline   Reply With Quote
Old 06-11-2004, 07:34 PM   #17
jugomkd75
Master of disaster
 
jugomkd75's Avatar
 
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
__________________

there is no handcraft without tool!
www.jugoars.com
jugomkd75 is offline   Reply With Quote
Old 06-11-2004, 07:37 PM   #18
jbum
Senior Member
 
jbum's Avatar
 
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
__________________
jbum is offline   Reply With Quote
Old 06-11-2004, 07:39 PM   #19
jbum
Senior Member
 
jbum's Avatar
 
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
__________________
jbum is offline   Reply With Quote
Old 06-11-2004, 07:39 PM   #20
jugomkd75
Master of disaster
 
jugomkd75's Avatar
 
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:
code:

// Utility function to determine if leap year
isLeapYear = function(year){
return ( year % 4 == 0 &&
(year % 100 != 0 || year % 400 == 0) );

}
// Utility function to determine number of days in month
// (including leap years)
kmDays = [31, 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31];
getMonthDays = function(year,mNbr) {
return kmDays[mNbr] + (mNbr == 1 && isLeapYear(year));
}

// Sample Usage
// print end of month for next 52 pay periods

// read the date from a string
datAry = otplata.split('.');
yr = datAry[2] + 2000; // full year
mn = datAry[1] - 1; // month in 0-11 format

//Round up the numbers;
kDelimiter = '.';
RoundToQuarter = function(x)
{
return Math.round(x*2)/2;
}

EncodeCurrency = function(x)
{
var ix = Math.floor(x);
var fx = x - ix;
fx = String(fx*100+100).substr(1);

if (ix >= 1000) {
ix = String(ix);
var len = ix.length;
var ngroups = 1 + Math.floor((len-4)/3);
var ox = '';
while (ngroups--)
{
ox = kDelimiter + ix.substr(len-3,3) + ox;
ix = ix.substr(0,len-3);
len -= 3;
}
ix += ox;
}
return ix + '.' + fx;
}
// drawing the table;
function CreateTable(thename, x, y, width, height, no_of_rows, no_of_columns, data, parent){
c=0;
for(i=0;i<no_of_rows;i++){
for(j=0;j<no_of_columns;j++){
// Create the new name for the text field
__name=thename+"_"+i+"_"+j;
// Create the text field
parent. createTextField(__name,c++,j*width+x,i*height+y,wi dth,height);
// Set the data and other settings
temp=parent[__name];
temp.text=data[i][j];
temp.background=false;
temp.border=false;
myformat = new TextFormat();
myformat.color = 0x000000;
myformat.font = "Arial";
myformat.size = 9;
myformat.align = "right";
temp.setTextFormat(myformat);
}
}
}

//insert values into tables
var p = stapka;
var r = 1+p/1200;
var z = iznos;
var a = z*Math.pow(r,rati)*(r-1)/(Math.pow(r, rati)-1);
myData = [];
for (i=0; i < rati+1; i++){
m=i+1;
k=z*p/1200;
b = a-k;
//spliting a numbers;
k= RoundToQuarter(k);
b= RoundToQuarter(b);
a= RoundToQuarter(a);
z= RoundToQuarter(z);

var dat = new Date(yr,mn,getMonthDays(yr,mn));
var datString = dat.getDate() + '.' + (dat.getMonth()+1) + '.' + dat.getFullYear();
if (++mn >= 12) {
mn = 0;
yr++;
}
myData[i] = [m, datString, EncodeCurrency(z), EncodeCurrency(b), EncodeCurrency(k), EncodeCurrency(a)];
z=z-b;
}
CreateTable("MyTable", 50, 120, 100, 14, rati, 6, myData, this);

__________________

there is no handcraft without tool!
www.jugoars.com
jugomkd75 is offline   Reply With Quote
Reply

Go Back   Flash Kit Community Forums > Flash Help > Flash ActionScript

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 08:28 PM.


internet.commerce
Be a Commerce Partner
 »  »  »  »  »  »  »
 »  »  »  »  »  »
 

    

Acceptable Use Policy

Internet.com
The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.