如何获取格式化数字的值

时间:2017-10-28 03:52:31

标签: swift formatting numbers int

我在Swift4中编码。有没有办法将格式化的数字转换为Int?如何获得有点这个格式化数字的值来进行计算?

let formattedNumber = "12,345,678,910" let newNumber = Int(formattedNumber)*2

2 个答案:

答案 0 :(得分:1)

您需要先删除逗号:

from django.views.generic import UpdateView
class JobUpdateView(UpdateView) 
    model = Job
    form_class = JobPostForm
    template_name = "Jobform.html"

    def get_object(self, *args, **kwargs):
       job = get_object_or_404(Job, pk=self.kwargs['pk'])
       return job

    def get_success_url(self, *args, **kwargs):
       return reverse("ulr_name_to_redirect_after_success")

答案 1 :(得分:1)

不完美。

extension String  {

  var intValue: Int   {

    let removedCharacter  = self.characters.map{String($0)}.filter{Int($0) != nil}.joined()
    print(removedCharacter)
    if !(removedCharacter.isEmpty)  {
        return Int(removedCharacter)!
    }
    return 0
  }

  func toInt(multiplyBy  : Int) -> String  {

    let formatter = NumberFormatter()
    formatter.numberStyle = .decimal
    formatter.groupingSeparator = ","
    let formattedString = formatter.string(for: intValue * multiplyBy)
    return formattedString!

  }
}

print("12,345,678,910".toInt(multiplyBy: 2))
>> 24,691,357,820