简体   繁体   English

如何在JavaScript中使用CPF格式的数字?

[英]How can I a number to CPF format in JavaScript?

I would like to format an integer to the CPF (Cadastro de Pessoas Físicas)(Brazilian ID Number) format. 我想将整数格式化为CPF(Cadastro dePessoasFísicas)(巴西ID号)格式。 I would like a function that takes an integer of 11 digits as an argument and returns a string formatted like this: 我想要一个函数,它取一个11位数的整数作为参数,并返回一个格式如下的字符串:

###.###.###-##

Example: 例:

formatToCPF(integer){     // integer=94256116885

    String formattedString

    //code

return formattedString    //returns "942.561.168-85"

You can first get the String value of integer and then format it with slice(). 您可以先获取整数的String值,然后使用slice()对其进行格式化。

s = integer.toString()
formattedString = s.slice(0,3)+"."+
    s.slice(3,6)+"."+
    s.slice(6,9)+"-"+
    s.slice(9,11)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM