How to add thousand separator in type Int and Float?
I have a question about add thousand separator.
I have three type of number string.
I find the answer in stack here.
But I try to use it, and failed to add thousand separator.
Have any idea to me? Thanks.
let str = "1000"
let string1 = "5000.000"
let string2 = "2000.0"
let convertStr = str.formattedWithSeparator //in playground, get error 「Value of type 'String' has no member 'formattedWithSeparator'」.
let convertStr1 = Float(string1)!.formattedWithSeparator //get error too.
let convertStr2 = Float(string2)!.formattedWithSeparator //get error too.
extension Formatter {
static let withSeparator: NumberFormatter = {
let formatter = NumberFormatter()
formatter.groupingSeparator = ","
formatter.numberStyle = .decimal
return formatter
}()
}
extension BinaryInteger {
var formattedWithSeparator: String {
return Formatter.withSeparator.string(for: self) ?? ""
}
}
ios swift
add a comment |
I have a question about add thousand separator.
I have three type of number string.
I find the answer in stack here.
But I try to use it, and failed to add thousand separator.
Have any idea to me? Thanks.
let str = "1000"
let string1 = "5000.000"
let string2 = "2000.0"
let convertStr = str.formattedWithSeparator //in playground, get error 「Value of type 'String' has no member 'formattedWithSeparator'」.
let convertStr1 = Float(string1)!.formattedWithSeparator //get error too.
let convertStr2 = Float(string2)!.formattedWithSeparator //get error too.
extension Formatter {
static let withSeparator: NumberFormatter = {
let formatter = NumberFormatter()
formatter.groupingSeparator = ","
formatter.numberStyle = .decimal
return formatter
}()
}
extension BinaryInteger {
var formattedWithSeparator: String {
return Formatter.withSeparator.string(for: self) ?? ""
}
}
ios swift
Why minus my question ?
– JimmyLee
Nov 26 '18 at 6:10
would you like to accept the answer if it solved the issue. This will help others to quickly identify the answer.
– Kamran
Nov 26 '18 at 7:06
add a comment |
I have a question about add thousand separator.
I have three type of number string.
I find the answer in stack here.
But I try to use it, and failed to add thousand separator.
Have any idea to me? Thanks.
let str = "1000"
let string1 = "5000.000"
let string2 = "2000.0"
let convertStr = str.formattedWithSeparator //in playground, get error 「Value of type 'String' has no member 'formattedWithSeparator'」.
let convertStr1 = Float(string1)!.formattedWithSeparator //get error too.
let convertStr2 = Float(string2)!.formattedWithSeparator //get error too.
extension Formatter {
static let withSeparator: NumberFormatter = {
let formatter = NumberFormatter()
formatter.groupingSeparator = ","
formatter.numberStyle = .decimal
return formatter
}()
}
extension BinaryInteger {
var formattedWithSeparator: String {
return Formatter.withSeparator.string(for: self) ?? ""
}
}
ios swift
I have a question about add thousand separator.
I have three type of number string.
I find the answer in stack here.
But I try to use it, and failed to add thousand separator.
Have any idea to me? Thanks.
let str = "1000"
let string1 = "5000.000"
let string2 = "2000.0"
let convertStr = str.formattedWithSeparator //in playground, get error 「Value of type 'String' has no member 'formattedWithSeparator'」.
let convertStr1 = Float(string1)!.formattedWithSeparator //get error too.
let convertStr2 = Float(string2)!.formattedWithSeparator //get error too.
extension Formatter {
static let withSeparator: NumberFormatter = {
let formatter = NumberFormatter()
formatter.groupingSeparator = ","
formatter.numberStyle = .decimal
return formatter
}()
}
extension BinaryInteger {
var formattedWithSeparator: String {
return Formatter.withSeparator.string(for: self) ?? ""
}
}
ios swift
ios swift
asked Nov 26 '18 at 5:56
JimmyLeeJimmyLee
13111
13111
Why minus my question ?
– JimmyLee
Nov 26 '18 at 6:10
would you like to accept the answer if it solved the issue. This will help others to quickly identify the answer.
– Kamran
Nov 26 '18 at 7:06
add a comment |
Why minus my question ?
– JimmyLee
Nov 26 '18 at 6:10
would you like to accept the answer if it solved the issue. This will help others to quickly identify the answer.
– Kamran
Nov 26 '18 at 7:06
Why minus my question ?
– JimmyLee
Nov 26 '18 at 6:10
Why minus my question ?
– JimmyLee
Nov 26 '18 at 6:10
would you like to accept the answer if it solved the issue. This will help others to quickly identify the answer.
– Kamran
Nov 26 '18 at 7:06
would you like to accept the answer if it solved the issue. This will help others to quickly identify the answer.
– Kamran
Nov 26 '18 at 7:06
add a comment |
2 Answers
2
active
oldest
votes
Number formatters do not start with a "number string"; they start with a number. Thus, for example, using the Formatter extension code you already have:
let n = 5000
let s = Formatter.withSeparator.string(for: n)
// s is now "5,000"
But let's say what you start with really is a string. Then you could say, for example:
let str = "5000"
let s = Formatter.withSeparator.string(for: Float(str)!)
// s is now "5,000"
Observe that the decimal information is lost in this process. If that were important to you, you'd need to add that requirement to the formatter itself. You are making a string, and you have to provide all information about how you want that string to look. For example:
let str = "5000.00"
let f = Formatter.withSeparator
f.minimumFractionDigits = 2
let s = f.string(for: Float(str)!)
// s is now "5,000.00"
If you omit the mimimumFractionDigits
info, you would get "5,000"
again; the original appearance of the string we started with is completely unimportant.
add a comment |
You can use this method
func currencyMaker(price: NSNumber) -> String {
let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = NumberFormatter.Style.decimal
numberFormatter.groupingSeparator = ","
let formattedNumber = numberFormatter.string(from: price)
return formattedNumber!
}
like this :
let myNumber1 = currencyMaker(price: 2000)
let myNumber2 = currencyMaker(price: 5983223)
the print is :
print(myNumber1) // 2,000
print(myNumber2) // 5,983,223
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53475445%2fhow-to-add-thousand-separator-in-type-int-and-float%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Number formatters do not start with a "number string"; they start with a number. Thus, for example, using the Formatter extension code you already have:
let n = 5000
let s = Formatter.withSeparator.string(for: n)
// s is now "5,000"
But let's say what you start with really is a string. Then you could say, for example:
let str = "5000"
let s = Formatter.withSeparator.string(for: Float(str)!)
// s is now "5,000"
Observe that the decimal information is lost in this process. If that were important to you, you'd need to add that requirement to the formatter itself. You are making a string, and you have to provide all information about how you want that string to look. For example:
let str = "5000.00"
let f = Formatter.withSeparator
f.minimumFractionDigits = 2
let s = f.string(for: Float(str)!)
// s is now "5,000.00"
If you omit the mimimumFractionDigits
info, you would get "5,000"
again; the original appearance of the string we started with is completely unimportant.
add a comment |
Number formatters do not start with a "number string"; they start with a number. Thus, for example, using the Formatter extension code you already have:
let n = 5000
let s = Formatter.withSeparator.string(for: n)
// s is now "5,000"
But let's say what you start with really is a string. Then you could say, for example:
let str = "5000"
let s = Formatter.withSeparator.string(for: Float(str)!)
// s is now "5,000"
Observe that the decimal information is lost in this process. If that were important to you, you'd need to add that requirement to the formatter itself. You are making a string, and you have to provide all information about how you want that string to look. For example:
let str = "5000.00"
let f = Formatter.withSeparator
f.minimumFractionDigits = 2
let s = f.string(for: Float(str)!)
// s is now "5,000.00"
If you omit the mimimumFractionDigits
info, you would get "5,000"
again; the original appearance of the string we started with is completely unimportant.
add a comment |
Number formatters do not start with a "number string"; they start with a number. Thus, for example, using the Formatter extension code you already have:
let n = 5000
let s = Formatter.withSeparator.string(for: n)
// s is now "5,000"
But let's say what you start with really is a string. Then you could say, for example:
let str = "5000"
let s = Formatter.withSeparator.string(for: Float(str)!)
// s is now "5,000"
Observe that the decimal information is lost in this process. If that were important to you, you'd need to add that requirement to the formatter itself. You are making a string, and you have to provide all information about how you want that string to look. For example:
let str = "5000.00"
let f = Formatter.withSeparator
f.minimumFractionDigits = 2
let s = f.string(for: Float(str)!)
// s is now "5,000.00"
If you omit the mimimumFractionDigits
info, you would get "5,000"
again; the original appearance of the string we started with is completely unimportant.
Number formatters do not start with a "number string"; they start with a number. Thus, for example, using the Formatter extension code you already have:
let n = 5000
let s = Formatter.withSeparator.string(for: n)
// s is now "5,000"
But let's say what you start with really is a string. Then you could say, for example:
let str = "5000"
let s = Formatter.withSeparator.string(for: Float(str)!)
// s is now "5,000"
Observe that the decimal information is lost in this process. If that were important to you, you'd need to add that requirement to the formatter itself. You are making a string, and you have to provide all information about how you want that string to look. For example:
let str = "5000.00"
let f = Formatter.withSeparator
f.minimumFractionDigits = 2
let s = f.string(for: Float(str)!)
// s is now "5,000.00"
If you omit the mimimumFractionDigits
info, you would get "5,000"
again; the original appearance of the string we started with is completely unimportant.
edited Nov 26 '18 at 6:14
answered Nov 26 '18 at 6:03
mattmatt
333k46544742
333k46544742
add a comment |
add a comment |
You can use this method
func currencyMaker(price: NSNumber) -> String {
let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = NumberFormatter.Style.decimal
numberFormatter.groupingSeparator = ","
let formattedNumber = numberFormatter.string(from: price)
return formattedNumber!
}
like this :
let myNumber1 = currencyMaker(price: 2000)
let myNumber2 = currencyMaker(price: 5983223)
the print is :
print(myNumber1) // 2,000
print(myNumber2) // 5,983,223
add a comment |
You can use this method
func currencyMaker(price: NSNumber) -> String {
let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = NumberFormatter.Style.decimal
numberFormatter.groupingSeparator = ","
let formattedNumber = numberFormatter.string(from: price)
return formattedNumber!
}
like this :
let myNumber1 = currencyMaker(price: 2000)
let myNumber2 = currencyMaker(price: 5983223)
the print is :
print(myNumber1) // 2,000
print(myNumber2) // 5,983,223
add a comment |
You can use this method
func currencyMaker(price: NSNumber) -> String {
let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = NumberFormatter.Style.decimal
numberFormatter.groupingSeparator = ","
let formattedNumber = numberFormatter.string(from: price)
return formattedNumber!
}
like this :
let myNumber1 = currencyMaker(price: 2000)
let myNumber2 = currencyMaker(price: 5983223)
the print is :
print(myNumber1) // 2,000
print(myNumber2) // 5,983,223
You can use this method
func currencyMaker(price: NSNumber) -> String {
let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = NumberFormatter.Style.decimal
numberFormatter.groupingSeparator = ","
let formattedNumber = numberFormatter.string(from: price)
return formattedNumber!
}
like this :
let myNumber1 = currencyMaker(price: 2000)
let myNumber2 = currencyMaker(price: 5983223)
the print is :
print(myNumber1) // 2,000
print(myNumber2) // 5,983,223
answered Nov 26 '18 at 7:27
aminamin
14410
14410
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53475445%2fhow-to-add-thousand-separator-in-type-int-and-float%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Why minus my question ?
– JimmyLee
Nov 26 '18 at 6:10
would you like to accept the answer if it solved the issue. This will help others to quickly identify the answer.
– Kamran
Nov 26 '18 at 7:06