How to convert float to string
I read a float from a file and will have to convert it to string. My problem here is that I am unsure of how many digits will be there after the decimal. I need to take the float exactly and convert it to string.
For ex:
1.10 should be converted to "1.10"
Also,
1.5 should be converted to "1.5"
Can someone suggest how to go about this?
string go floating-point precision
add a comment |
I read a float from a file and will have to convert it to string. My problem here is that I am unsure of how many digits will be there after the decimal. I need to take the float exactly and convert it to string.
For ex:
1.10 should be converted to "1.10"
Also,
1.5 should be converted to "1.5"
Can someone suggest how to go about this?
string go floating-point precision
2
Possible duplicate of How to format floating point numbers into a string using Go
– nilsocket
Nov 15 '18 at 6:05
add a comment |
I read a float from a file and will have to convert it to string. My problem here is that I am unsure of how many digits will be there after the decimal. I need to take the float exactly and convert it to string.
For ex:
1.10 should be converted to "1.10"
Also,
1.5 should be converted to "1.5"
Can someone suggest how to go about this?
string go floating-point precision
I read a float from a file and will have to convert it to string. My problem here is that I am unsure of how many digits will be there after the decimal. I need to take the float exactly and convert it to string.
For ex:
1.10 should be converted to "1.10"
Also,
1.5 should be converted to "1.5"
Can someone suggest how to go about this?
string go floating-point precision
string go floating-point precision
edited Nov 16 '18 at 8:24
Flimzy
38.9k106597
38.9k106597
asked Nov 15 '18 at 5:12
Nagireddy HanishaNagireddy Hanisha
328525
328525
2
Possible duplicate of How to format floating point numbers into a string using Go
– nilsocket
Nov 15 '18 at 6:05
add a comment |
2
Possible duplicate of How to format floating point numbers into a string using Go
– nilsocket
Nov 15 '18 at 6:05
2
2
Possible duplicate of How to format floating point numbers into a string using Go
– nilsocket
Nov 15 '18 at 6:05
Possible duplicate of How to format floating point numbers into a string using Go
– nilsocket
Nov 15 '18 at 6:05
add a comment |
2 Answers
2
active
oldest
votes
Use strconv.FormatFloat like such:
s := strconv.FormatFloat(3.1415, 'E', -1, 64)
fmt.Println(s)
Outputs
3.1415
add a comment |
Convert float to string
FormatFloat converts the floating-point number f to a string, according to the format fmt and precision prec. It rounds the result assuming that the original was obtained from a floating-point value of bitSize bits (32 for float32, 64 for float64).
func FormatFloat(f float64, fmt byte, prec, bitSize int) string
f := 3.14159265
s := strconv.FormatFloat(f, 'E', -1, 64)
fmt.Println(s)
Output is "3.14159265"
Another method is by using fmt.Sprintf
s := fmt.Sprintf("%f", 123.456)
fmt.Println(s)
Output is "123.456000"
Check the code on play ground
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%2f53312828%2fhow-to-convert-float-to-string%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
Use strconv.FormatFloat like such:
s := strconv.FormatFloat(3.1415, 'E', -1, 64)
fmt.Println(s)
Outputs
3.1415
add a comment |
Use strconv.FormatFloat like such:
s := strconv.FormatFloat(3.1415, 'E', -1, 64)
fmt.Println(s)
Outputs
3.1415
add a comment |
Use strconv.FormatFloat like such:
s := strconv.FormatFloat(3.1415, 'E', -1, 64)
fmt.Println(s)
Outputs
3.1415
Use strconv.FormatFloat like such:
s := strconv.FormatFloat(3.1415, 'E', -1, 64)
fmt.Println(s)
Outputs
3.1415
edited Nov 15 '18 at 14:21
answered Nov 15 '18 at 5:28
UllaakutUllaakut
1,502714
1,502714
add a comment |
add a comment |
Convert float to string
FormatFloat converts the floating-point number f to a string, according to the format fmt and precision prec. It rounds the result assuming that the original was obtained from a floating-point value of bitSize bits (32 for float32, 64 for float64).
func FormatFloat(f float64, fmt byte, prec, bitSize int) string
f := 3.14159265
s := strconv.FormatFloat(f, 'E', -1, 64)
fmt.Println(s)
Output is "3.14159265"
Another method is by using fmt.Sprintf
s := fmt.Sprintf("%f", 123.456)
fmt.Println(s)
Output is "123.456000"
Check the code on play ground
add a comment |
Convert float to string
FormatFloat converts the floating-point number f to a string, according to the format fmt and precision prec. It rounds the result assuming that the original was obtained from a floating-point value of bitSize bits (32 for float32, 64 for float64).
func FormatFloat(f float64, fmt byte, prec, bitSize int) string
f := 3.14159265
s := strconv.FormatFloat(f, 'E', -1, 64)
fmt.Println(s)
Output is "3.14159265"
Another method is by using fmt.Sprintf
s := fmt.Sprintf("%f", 123.456)
fmt.Println(s)
Output is "123.456000"
Check the code on play ground
add a comment |
Convert float to string
FormatFloat converts the floating-point number f to a string, according to the format fmt and precision prec. It rounds the result assuming that the original was obtained from a floating-point value of bitSize bits (32 for float32, 64 for float64).
func FormatFloat(f float64, fmt byte, prec, bitSize int) string
f := 3.14159265
s := strconv.FormatFloat(f, 'E', -1, 64)
fmt.Println(s)
Output is "3.14159265"
Another method is by using fmt.Sprintf
s := fmt.Sprintf("%f", 123.456)
fmt.Println(s)
Output is "123.456000"
Check the code on play ground
Convert float to string
FormatFloat converts the floating-point number f to a string, according to the format fmt and precision prec. It rounds the result assuming that the original was obtained from a floating-point value of bitSize bits (32 for float32, 64 for float64).
func FormatFloat(f float64, fmt byte, prec, bitSize int) string
f := 3.14159265
s := strconv.FormatFloat(f, 'E', -1, 64)
fmt.Println(s)
Output is "3.14159265"
Another method is by using fmt.Sprintf
s := fmt.Sprintf("%f", 123.456)
fmt.Println(s)
Output is "123.456000"
Check the code on play ground
edited Nov 16 '18 at 9:01
answered Nov 15 '18 at 7:23
ASHWIN RAJEEVASHWIN RAJEEV
241211
241211
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%2f53312828%2fhow-to-convert-float-to-string%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
2
Possible duplicate of How to format floating point numbers into a string using Go
– nilsocket
Nov 15 '18 at 6:05