angular->管道pipe 格式化文件大小

1.新建filesize管道

import {Pipe, PipeTransform} from '@angular/core';

@Pipe({
  name: 'fileSizeFormat'
})
export class FileSizeFormatPipe implements PipeTransform {

  transform(value: number, args?: any): string {
    return this._format(value);
  }

  private _format(size: number): string {
    if (size <= 0) {
      return '0 Bytes';
    }
    const unit = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
    const idx = Math.floor(Math.log(size) / Math.log(1024));
    const s: number = size / Math.pow(1024, idx);
    return `${s.toFixed(2)} ${unit[idx]}`;
  }

}
View Code

2.在界面上调用

<div>{{ data.FileSize|fileSizeFormat }}</div>

 

posted @ 2020-03-03 10:51  面包_girl  阅读(344)  评论(0编辑  收藏  举报
/* 鼠标点击文字特效 */