我正在开发一个 AIR 或 Android 应用程序,该应用程序从 CSV 文件加载数据并在动态文本框中显示该数据。
CSV 文件中的某些字段是数字,我无法将它们设置为货币格式并显示在动态文本字段中。
import flash.events.MouseEvent;
import flash.text.*;
import flashx.textLayout.formats.Float;
function internationalFormat(number:Number):String {
return String(number).replace(".", ",").replace(String(number).indexOf(".") > -1 ?/(?<=\d)(?=(\d\d\d)+(?!\d)(?:\,\d*))/g : /(?<=\d)(?=(\d\d\d)+(?!\d))/g, ".").replace(/\.{2,}/g, ".");
}
var toFixed:Function = function(number, factor) {
return (Math.round(number * factor)/factor);
}
var tf1:TextFormat = new TextFormat();
tf1.font ="Arial Rounded MT Bold";
tf1.size = 20;
tf1.align = "left";
var img1Requestf:URLRequest = new URLRequest("http://www.maineventjamaica.com/digicelpad/photo.png");
var img1Loaderf:Loader = new Loader();
img1Loaderf.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
img1Loaderf.load(img1Requestf);
_1.addChild(img1Loaderf);
stop();
//CSV PATH
var myRequest1:URLRequest = new URLRequest("http://www.maineventjamaica.com/digicelpad/table_database_final.csv");
//Load ARRAY
var loadedData1:Array = [];
//loader variable
var myLoader1 = new URLLoader();
myLoader1.addEventListener(Event.COMPLETE, onload1);
myLoader1.load(myRequest1);
function onload1(Event):void
{
//Now, lets put each row into its own array. To do that, we split the data by /\r\n|\n|\r/, which is a RegExp pattern that works with CSV files that can be created on all platforms (Windows puts \r\n between variables, Mac puts \r and Linux puts \n).
loadedData1 = myLoader1.data.split(/\r\n|\n|\r/);
for (var i:int=0; i<loadedData1.length; i++){
loadedData1[i] = loadedData1[i].split(",");
//Number(loadedData1[i].attributes.time.toString())
}
//var sprice:Number = loadedData1[1][5];
//var test:Number = loadedData1[1][5];
//Testing datatype convertion
//var myInt:int = parseInt(loadedData1[1][5]);
//var myFloat:Number = parseFloat(loadedData1[1][5]);
var number:Number = Number(loadedData1[1][5]);
trace(internationalFormat(number));
//-trace(loadedData1[1][5]);
myTXTh0.text = Number("$" + trace(internationalFormat(number)));
trace(myTXTh0.text);
myTXTh1.text = String("$" + loadedData1[1][3]);
myTXTh2.text = String(loadedData1[1][6]);
//myTXTh3.text = String(loadedData1[1][8]);
//myTXTh4.text = String(loadedData1[1][7]);
myTXTh5.text = String(loadedData1[1][4]);
myTXTh0.defaultTextFormat = tf1;
myTXTh1.defaultTextFormat = tf1;
myTXTh2.defaultTextFormat = tf1;
//myTXTh3.defaultTextFormat = tf1;
//myTXTh4.defaultTextFormat = tf1;
myTXTh5.defaultTextFormat = tf1;
//myTXTh5s.defaultTextFormat = tf1;
}