如何将数字转换为特殊格式

时间:2018-12-17 09:15:21

标签: javascript

我有一个电话号码:<div class="image-container"> <div class="cycle-slideshow" data-cycle-fx=carousel data-cycle-timeout=0 data-cycle-carousel-visible=2 data-cycle-caption="#custom-caption2" data-cycle-caption-template=" <h1 id='custom-caption2' class='sourcesans font-weight-bold' style='color:#9e2424;font-size:35px'>{{alt}}</h1> <p class='sourcesans' style='font-size:18px;padding-bottom: 10px; color: white;font-weight: 300'>{{cycleTitle}}</p> <p style='font-weight:900; font-size: 14px'> <a target='_self' style='color:white;text-decoration-line:none;' href='{{name}}'> See More</a> </p>" data-cycle-pager="#custom-pager" data-cycle-pager-template="<a target='_self' style='color:white;font-size:22px;font-weight: 300;' class='nav-link graybar-nav sourcesans' href='{{name}}'>{{alt}}</a>" data-cycle-pager-event="mouseover"> <h1 class="sourcesans" style="font-size:50px;color: #fff;font-weight: 300;padding-top: 10px"> Our Services</h1> <!-- <img data-cycle-title="" style="padding-top:160px; width: 100%; height:700px; z-index: -1;" src="img/sld-services.jpg"> --> <img name="services/asset-management.html" data-cycle-title="The Asset Management department was established to provide services to institutional and individual investors. With our team of experienced fund managers, we are able to provide tailored products and services to both..." alt="Asset Management" style="padding-top:160px; width: 1192px; height: 700px; z-index: -1;" src="{{'assets/img/sld-asm.jpg'|theme }}"> <img name="services/trusteeship.html" data-cycle-title="In Corporate Trust Services, we act as trustees to consortium and syndications, bonds of state and local governments, Unit Trust Schemes, Debenture Stocks etc. In lending transactions, we represent mainly the interest of the lenders by... " alt="Trusteeship" style="padding-top:160px; width: 1192px;height: 700px; z-index: -1;" src="{{'assets/img/sld-trs.jpg'|theme }}"> <img name="services/stockbroking.html" data-cycle-title="Associated Asset Manager LTD is a wholly owned subsidiary of SAMTL. AAML is a member of the Nigerian Stock Exchange (NSE) and is licensed by the Securities and Exchange Commission (SEC) in. AAML remains a leading player in Nigeria’s..." alt="Stockbroking" style="padding-top:160px; width: 1192px; height: 700px; z-index: -1;" src="{{'assets/img/sld-sb.jpg'|theme }}"> <img name="services/advisory.html" data-cycle-title="We deliver strategic management, corporate finance and Advisory services to clients relating to restructuring (corporate restructuring, debt restructuring, financial restructuring), Mergers & Acquisitions (M&A)..." alt="Advisory" style="padding-top:160px; width: 1192px; height: 700px; z-index: -1;" src="{{'assets/img/sld-ad.jpg'|theme }}"> </div> </div> <div style="padding-bottom:70px" class="row"> <div class="col-md-4"> <div id="custom-caption" class="cycle-caption"> </div> </div> <div style="padding-top:180px" class="col-md-1"> <div class="row"> <ion-icon data-cycle-cmd="prev" style="color:#9a2425" size="large" name="arrow-round-back"></ion-icon> <ion-icon data-cycle-cmd="next" style="color:#9a2425" size="large" name="arrow-round-forward"></ion-icon> </div> </div> <div class="col-md-7"> <div class="cycle-slideshow" data-cycle-fx=carousel data-cycle-timeout=0 data-cycle-carousel-visible=2 data-cycle-caption="#custom-caption" data-cycle-caption-template="{{alt}} <p class='h5 font-weight-light'>{{cycleTitle}}</p>" data-cycle-pause-on-hover="true"> <img src="{{'assets/img/associated-asset-managers-small.jpg'|theme }}" data-cycle-title="Associated Asset Managers Limited (AAML) was licensed in June 2006 as a wholly owned subsidiary of Associated Discount House who recently divested, based on the CBN’s directives We execute trades on behalf of our clients on the floor of The Nigerian Stock Exchange and ensure that their investment objectives are met." alt="<h2 class=’sourcesans’ style=’font-size:30px;color:#9a2425;font-weight:600;padding-bottom:30px’> <a target='_self' style='color:inherit;text-decoration-line:none' href='subs/asset-management-trustees.html'> Associated Asset Managers Limited </a> </h2>"> <img src="{{'assets/img/Leasing.jpg'|theme }}" data-cycle-title="SAMTL Leasing Ltd is a fully owned subsidiary of SAMTL established to provide first class, affordable and convenient lease options to individuals and corporate organisations. These include: Fleet and logistics management (operating lease) vehicle Leasing (executive, pool, buses, trucks and equipment), consumer and business lease, and driver recruitment and training." alt="<h2 class=’sourcesans’ style=’font-size:30px;color:#9a2425;font-weight:600;padding-bottom:30px’> <a target='_self' style='color:inherit;text-decoration-line:none' href='subs/samtl-leasing.html'> SAMTL Leasing Limited </a> </h2>"> </div> </div> </div>

我想将其转换为:410226497.3017611

这是我当前的代码。

410.226

但是以上方法不起作用。有什么建议吗?

4 个答案:

答案 0 :(得分:1)

如果执行代码,则会看到:

输入:

var data = 410226497.3017611;
(data/1000000).toLocaleString('en-US', {minimumFractionDigits: 1, maximumFractionDigits: 1})

输出:

"410.2"

toLocaleString的文档中,有关于minimumFractionDigitsmaximumFractionDigits的描述。

您可以将maximumFractionDigits更改为3。然后:

输入:

(410226497.3017611/1000000).toLocaleString('en-US', {minimumFractionDigits: 1, maximumFractionDigits: 3})

输出:

"410.226"

答案 1 :(得分:1)

您可以像这样使用Number.prototype.toFixed()

(data / 1000000).toFixed(3);

这是什么限制了浮点值的小数点后允许的位数-因此,如果我们这样做:

Math.PI.toFixed(4)

只需要传递浮点数(3.141592 ...),然后将小数点后第四位之后的所有内容都切成片,因此返回3.1415。

希望这会有所帮助!

答案 2 :(得分:0)

如果您始终需要3个十进制数字,则可以尝试使用

var a = 410226497.3017611;
(a/1000000).toFixed(3);  # this will return "410.226"

答案 3 :(得分:0)

首先,您需要检索数字的前6位:

const a = 410226497.3017611;
const firstSix = (a+'').slice(0,6) // "410226"

然后,将其解析回数字并除以1000:

const final = Number(firstSix)/1000

console.log(final) // 410.226