js格式转换
//1、保留整数
function showInteger(value,row,index){
if(value!=null && value!="" && typeof(value)!="undefined"){
var strint = Math.round(value);
return strint;
}else{
return "";
}
}
//2、保留2位小数,如:2,会在2后面补上00.即2.00
function toDecimal2(value,row,index) {
if(value!=null && value!="" && typeof(value)!="undefined"){
var f = parseFloat(value);
if (isNaN(f)) {
return false;
}
var f = Math.round(value*100)/100;
var s = f.toString();
var rs = s.indexOf('.');
if (rs < 0) {
rs = s.length;
s += '.';
}
while (s.length <= rs + 2) {
s += '0';
}
return s;
}else{
return "";
}
}
//3、保留4位小数,如:2,会在2后面补上0000.即2.0000
function toDecimal4(value,row,index) {
if(value!=null && value!="" && typeof(value)!="undefined"){
var f = parseFloat(value);
if (isNaN(f)) {
return false;
}
var f = Math.round(value*10000)/10000;
var s = f.toString();
var rs = s.indexOf('.');
if (rs < 0) {
rs = s.length;
s += '.';
}
while (s.length <= rs + 4) {
s += '0';
}
return s;
}else{
return "";
}
}
//4、将数字转化为金额,将1234567.00转换为1,234,567.00
function changeToMoney1(value,row,index){
if(value!=null && value!="" && typeof(value)!="undefined"){
value = parseFloat((value + "").replace(/[^\d\.-]/g, "")).toFixed(2) + "";
var l = value.split(".")[0].split("").reverse(),
r = value.split(".")[1];
t = "";
for(i = 0; i < l.length; i ++ )
{
t += l[i] + ((i + 1) % 3 == 0 && (i + 1) != l.length ? "," : "");
}
var tttt=t.split("").reverse().join("") + "." + r;
return t.split("").reverse().join("") + "." + r;
}else{
return "";
}
}
//5、将数字转化为金额保留两位小数,将1234567.00转换为¥1234567.00
function changeToMoney2(value,row,index){
if(value!=null && value!="" && typeof(value)!="undefined"){
var f = parseFloat(value);
if (isNaN(f)) {
return false;
}
var f = Math.round(value*100)/100;
var s = f.toString();
var rs = s.indexOf('.');
if (rs < 0) {
rs = s.length;
s += '.';
}
while (s.length <= rs + 2) {
s += '0';
}
return "¥" +s;
}else{
return "";
}
}
//6、将数字转化为金额,将1234567.00转换为1,234,567.00
function changeToMoney3(value,row,index){
if(value!=null && value!="" && typeof(value)!="undefined"){
value = parseFloat((value + "").replace(/[^\d\.-]/g, "")).toFixed(2) + "";
var l = value.split(".")[0].split("").reverse(),
r = value.split(".")[1];
t = "";
for(i = 0; i < l.length; i ++ )
{
t += l[i] + ((i + 1) % 3 == 0 && (i + 1) != l.length ? "," : "");
}
var tttt="¥" + t.split("").reverse().join("") + "." + r;
return "¥" + t.split("").reverse().join("") + "." + r;
}else{
return "";
}
}
//7、转化为百分比1,将0.123转化为12%
function toPercent1(value,row,index){
if(value!=null && value!="" && typeof(value)!="undefined"){
var strData = value*100;
strData = Math.round(strData);
var p = strData.toString()+"%";
return p;
}else{
return "";
}
}
//8、转化为百分比2,将0.123转化为12.30%
function toPercent2(value,row,index){
if(value!=null && value!="" && typeof(value)!="undefined"){
var strData = parseFloat(value)*10000;
strData = Math.round(strData);
strData/=100.00;
strData=strData.toFixed(2);
var ret = strData.toString()+"%";
return ret;
}else{
return "";
}
}
//9、将时间格式转换为2009年6月15日
function dateFormatter2(value,row,index){
if(value!=null && value!="" && typeof(value)!="undefined"){
var date = new Date(value);
var year = date.getFullYear();
var month = date.getMonth()+1; //js从0开始取
var day = date.getDate();
return year+"年"+month+"月"+day+"日";
}else{
return "";
}
}
// 10、将日期类型转换成字符串型格式yyyy-MM-dd
function ChangeDateToString(value,row,index) {
if(value!=null && value!="" && typeof(value)!="undefined"){
var Year=0;
var Month=0;
var Day=0;
var CurrentDate="";
//初始化时间
var date = new Date(value);
Year = date.getFullYear();
Month = date.getMonth()+1;
Day = date.getDate();
CurrentDate = Year + "-";
if (Month >= 10 ){
CurrentDate = CurrentDate + Month + "-";
}else{
CurrentDate = CurrentDate + "0" + Month + "-";
}
if (Day >= 10 ){
CurrentDate = CurrentDate + Day ;
}else{
CurrentDate = CurrentDate + "0" + Day ;
}
return CurrentDate;
}else{
return "";
}
}
// 11、将日期类型转换成字符串型格式yyyy-MM-dd hh:mm:ss
function ChangeTimeToString(value,row,index){
if(value!=null && value!="" && typeof(value)!="undefined"){
var Year=0;
var Month=0;
var Day=0;
var Hour = 0;
var Minute = 0;
var CurrentDate="";
//初始化时间
var date = new Date(value);
Year = date.getFullYear();
Month = date.getMonth()+1;
Day = date.getDate();
Hour = date.getHours();
Minute = date.getMinutes();
Second = date.getSeconds();
CurrentDate = Year + "-";
if (Month >= 10 ){
CurrentDate = CurrentDate + Month + "-";
}else{
CurrentDate = CurrentDate + "0" + Month + "-";
}
if (Day >= 10 ){
CurrentDate = CurrentDate + Day ;
}else{
CurrentDate = CurrentDate + "0" + Day ;
}
if(Hour >=10){
CurrentDate = CurrentDate + " " + Hour ;
}else{
CurrentDate = CurrentDate + " 0" + Hour ;
}
if(Minute >=10){
CurrentDate = CurrentDate + ":" + Minute ;
}else{
CurrentDate = CurrentDate + ":0" + Minute ;
}
if(Second >=10){
CurrentDate = CurrentDate + ":" + Second ;
}else{
CurrentDate = CurrentDate + ":0" + Second ;
}
return CurrentDate;
}else{
return "";
}
}
//12、将时间格式转换为2009年6月15日15时44分50秒
function dateFormatter1(value,row,index){
if(value!=null && value!="" && typeof(value)!="undefined"){
var date = new Date(value);
var year = date.getFullYear();
var month = date.getMonth()+1; //js从0开始取
var day = date.getDate();
var hour = date.getHours();
var minutes = date.getMinutes();
var second = date.getSeconds();
return year+"年"+month+"月"+day+"日"+hour+"时"+minutes +"分"+second+"秒";
}else{
return "";
}
}