Is there way to modify Number class - actionscript-3

At clasic windows calculator, nubers are separated by comma, like: 4,5
I wish to have it same in flash, but:
Number("4,5") //output: NaN
Is there any way modify Number class to get this result?
Number("4,5") //output: 4.5
Number("4.5") //output: 4.5
Also string with spaces can be considered as number...
trace(Number("10000" )) //output: 10000
trace(Number("10 000")) //output: NaN
one more example about numbers which can be fixed:
trace(4.3 - 1.1) //output 3.1999999999999997
trace(Number(4.3 - 1.1).toFixed(1)) //output: 3.2

Instead of using Number , use a function and send the values to it , just like this
function convertNumber(var:String):Number
{
var arr:Array;
arr = var.split(",");
if(arr.length >0)
{
var sendStr:String = arr[0] + "." + arr[1];
return Number(sendStr);
}
arr = var.split(" ");
if(arr.length >0)
{
var sendStr:String = arr[0] + arr[1];
return Number(sendStr);
}
return Number(var)
}
use convertNumber(4,5) to get output as 4.5

Related

Get an array of all Numbers inside a String

How can I get an array of all Numbers (if any) inside a String?
So that this:
var txt: String = "So 1 and 22 plus33 = (56) and be4 100 is 99!";
trace(getNumAry(txt));
Output this:
1,22,33,56,4,100,99
Regular Expressions is the answer you are looking for. You need something like that (not tested, yet the idea should be correct):
var source:String = "So 1 and 22 plus33 = (56) and be4 100 is 99!"
// The pattern \d+ instructs to find one or more consequent decimal digits.
// That also means that before each match there will be a non-digit character
// or the beginning of the text, and after the match will also be a non-digit
// or the end of the text.
// The [g]lobal flag is for searching multiple matches.
var re:RegExp = /\d+/g;
// Search for all the matches.
var result:Array = source.match(re);
Without the elaborate commenting the code could be reduced to this simple one-liner:
var result:Array = "So 1 and 22 plus33 = (56) and be4 100 is 99!".match(/\d+/g);
Please keep in mind that this search will return an Array of Strings so if you want them as ints you need to take some additional steps.
This is the working solution I have come up with:
function getNumAry(txt:String):Array {
var res:Array = new Array();
var str: String = ""
//Non-Number Chars to Dot
for (var i:int = 0; i < txt.length; i++) {
if(isNum(txt.substr(i,1))){
str += txt.substr(i, 1);
} else {
str += ".";
}
}
trace(txt);
trace(str);
//Spaces to Dot
str = str.split(" ").join(".")
trace(str);
//Dots to Single Dot
while (str.indexOf("..") != -1) {
str = str.split("..").join(".");
}
trace(str);
//Remove first Dot
if (str.indexOf(".") == 0 ) {
str = str.substr(1);
}
trace(str);
//Remove last Dot
if (str.lastIndexOf(".") == str.length-1 ) {
str = str.substr(0,str.length-1);
}
trace(str);
//get Nums if any
if (str != "" && str != ".") {
res = str.split(".");
}
return res;
}
function isNum(chr: String):Boolean {
return !isNaN(Number(chr));
}
if you run this:
var txt: String = "So 1 and 22 plus33 = (56) and be4 100 is 99!";
trace(getNumAry(txt));
This is the step-by-step trace of what you get:
So 1 and 22 plus33 = (56) and be4 100 is 99!
.. 1 ... 22 ....33 . .56. ... ..4 100 .. 99.
...1.....22.....33....56........4.100....99.
.1.22.33.56.4.100.99.
1.22.33.56.4.100.99.
1.22.33.56.4.100.99
1,22,33,56,4,100,99
Wondering if there has been an easier way?! :)

As3 set set end-Date using value in array

i want to make end-subscription after admin enter the value/date into text_box then it will push to array,after that i want when time reach the date that been enter the application will exit.
But I don't know the method.
below is my code
var my_date:Date;
var my_timer:Timer=new Timer(1000);
my_timer.addEventListener(TimerEvent.TIMER, onTimer);
my_timer.start();
var tarikh:Array = new Array();
tarikh.hari = [0];
tarikh.bulan = [1];
tarikh.tahun = [2];
my_date = new Date();
function onTimer(e:TimerEvent):void
{
my_date = new Date();
//trace(my_date.date + ":" + my_date.month + " : " + my_date.fullYear );
}
btnCuba.addEventListener(MouseEvent.CLICK,MMM);
function MMM (event:MouseEvent):void
{
tarikh.hari = String(hari.text);
tarikh.bulan = String(bulan.text);
tarikh.tahun = String(tahun.text) ;
tarikh.push(hari,bulan,tahun);
setHours();
trace(tarikh.hari + "" + tarikh.bulan + "" + tarikh.tahun);
}
function setHours():void
{
if (my_date.date == tarikh.hari && my_date.month == tarikh.hari && my_date.fullYear == tarikh.hari && && my_date.minutes == 01)
{
NativeApplication.nativeApplication.exit();
}
else
{
my_timer.start();
}
}
tarikh is an array. Arrays are assigned to using the array index access operator. When you want to store data in an array, you do it like this:
array_name[index] = value
or in this case,
tarikh[0] = hari;
Looking at the code, tarikh seems to mean date, hari, bulan and tahun seem to mean day, month and year. The Date class doesn't have these properties (you'll have to translate them to English); alternatively, an easier method of comparing dates is just to compare the getTime() function returns by the two date objects:
if(my_date.getTime() == tarikh.getTime()) { NativeApplication.nativeApplication.exit(); }
The problem is that tarikh is not a Date object now, but is an Array. Arrays don't have any properties by themselves (apart from length), so maybe you're looking for a tarikh being a Date object? You could try making tarikh as a Date object with the values like so:
tarikh = new Date(hari.text + " " + bulan.text + " " + tahun.text); if the text values are letters (like Tue Feb 3 2005), otherwise if they are numbers or integers then try casting it and using it in the appropriate parameter.

Utilities.formatString not working as expected

I wrote a small script in order to have an incremental ID composed by string character by using the function Utilities.formatString
Example: RQ001 -> RQ002 --> RQ00n
function myFunction() {
var str = "RQ001";
var res = str.substring(2, 5); 'ok
res=Number(res)+1; 'ok
res=res.toString(); 'ok
res = "RQ" & Utilities.formatString("000", res); 'not working }
The results is "0".
Thank you in advance for your help.
& is not string concatenation operator. Use "+" instead.
You have two issues. You need to use "+" for string concatenation and your method for creating your string isn't properly returning "002" like you want.
function myFunction() {
var str = "RQ001";
var res = Number(str.substring(2, 5)) + 1;
res = "000" + String(res);
res = "RQ" + res.substring(res.length - 3, res.length);
}
Use substring() to format your string's number and then append the "RQ" to the beginning afterwards.
You can also format the function to take the original key as a parameter. This way, you can say something like newKey = incrementKey(oldKey).
function incrementKey(str) {
var res = Number(str.substring(2, 5)) + 1;
res = "000" + String(res);
res = "RQ" + res.substring(res.length - 3, res.length);
return res;
}

I have a function that changes months into numbers, but I'd like to add 0 before the month's number

I have a date like:
19/août/2016 (août = august)
And I have the following function which changes the month into a number:
function swapMonthForNumber(str:String):String {
//do the same line of code for every item in the array
for(var i:int=0;i<months.length;i++){
//i is the item, which is 0 based, so we have to add 1 to make the right month number
str = str.replace(months[i],String(+i+1));
}
//return the updated string
return str;
}
str = swapMonthForNumber(mySharedObject.data.theDate);
trace("Php will use this date :"+str);
So str will be 19/8/2016, but I want str to be 19/08/2016 (adding a 0 before the 8).
How can I do this?
Check out the reference of the Date class!
If forgot to mention this link : flash.globalization.DateTimeFormatter
DateTimeFormatter(requestedLocaleIDName:String, dateStyle:String = "long", timeStyle:String = "long")
Here is an example.
import flash.globalization.DateTimeFormatter;
var df:DateTimeFormatter = new DateTimeFormatter(LocaleID.DEFAULT, DateTimeStyle.SHORT, DateTimeStyle.NONE);
var currentDate:Date = new Date(2016,7,19);
var shortDate:String = df.format(currentDate);
trace (shortDate);
// output : 19/08/2016
DateTimeStyle
LocaleID
Adding leading zeros to a number is commonly called zero padding.
Below is a function to do this, from the answer here.
public function zeroPad(number:int, width:int):String {
var ret:String = ""+number;
while( ret.length < width )
ret="0" + ret;
return ret;
}
In your swapMonthForNumber function, in the for loop, swap the code for this:
var month = zeroPad(i + 1, 2);
str = str.replace(months[i], month);

number manipulation in angular

I was trying to convert some number automatically from x=1 to x=00001 so that x takes 5 spaces in total. If x=20.2, I want to format x to be 00020. How can I do that?
I am using html and angularjs. Thanks.
Is this what you want?
function convert(input)
{
var a = Math.floor(input).toString();
return ("00000").substr(0, 5 - a.length) + a;
}
Example:
console.log(convert(20.2)); // 00020
write this filter to achieve the logic
<div>{{number | decimalConvertion:5}}</div>
filter('decimalConvertion', function() {
return function(str, max){
function pad(str, max){
str = str.toString();
return str.length < max ? pad("0" + str, max) : str;
}
return pad(str,8);
};
}
for your reference jsfiddle link