You are not logged in. Please login at www.codechef.com to post your questions!

×

Conversion from integer/floating-point to string

Please tell me how can I convert integers such as 5,7,89,etc. or floating-ppoints like 5.90,7.45,etc. to strings like "5","5.90",etc.

asked 07 Mar '15, 19:32

anupam_datta's gravatar image

4★anupam_datta
469529
accept rate: 7%


Hello Anupam, it's very easy to convert ..take a look at this code below.

class Conversion{

     public static void main(String args[]){
                int x= 2;

                float y = 2.95;

                String str_int = 2+"";

                String str_float = 2.95+"";

               //now you can print it..

                System.out.println("Int to  String "+str_int+" Float to String "+str_float);

          }

}

link

answered 07 Mar '15, 19:48

shubh_coder's gravatar image

1★shubh_coder
14
accept rate: 0%

This might be of your use, http://ideone.com/NjqIMR Jst using basic sprintf() function.

link

answered 07 Mar '15, 19:58

damn_me's gravatar image

3★damn_me
2.6k21336
accept rate: 24%

As I am new to the site, I did not see what language you wanted this in so I am going to post what I would do in C++. Since C++ 11 has a new function of putting things to strings, see code below, it is easier to convert things now!

#include <string>

int a = 12;
float b = 3.1459;
std::string str = to_string(a);
std::string str2 = to_string(b);
link

answered 07 Mar '15, 20:05

liability's gravatar image

0★liability
1
accept rate: 0%

I want the code in c++4.3.2

link

answered 07 Mar '15, 20:43

anupam_datta's gravatar image

4★anupam_datta
469529
accept rate: 7%

toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×17

question asked: 07 Mar '15, 19:32

question was seen: 1,457 times

last updated: 07 Mar '15, 20:43