← Back to C++ Course | Chapter 6: Arrays & Strings | Lesson 10 of 15

C++ में Strings और Numbers Convert करना

std::to_string एक number को एक string में convert करता है, और stoi और stod जैसे functions एक string को वापस एक number में convert करते हैं, text और numeric form के बीच data move करने के लिए ज़रूरी दो directions।
Syntax
cpp
std::string text = std::to_string(number);

int number = std::stoi(text);

एक Number को एक String में Convert करना

std::to_string C++ के किसी भी built-in numeric type को एक std::string में convert करता है, इसका standard text representation produce करते हुए, concatenate या display होने के लिए ready।

उदाहरण: Converting a Number to a String

cpp
// Include std::cout and std::cin
#include <iostream>
// Include std::string
#include <string>

// Program execution starts in main()
int main() {
	// Declare age and set it to 25
	int age = 25;
	std::string text = std::to_string(age);
	// Print to the console with cout
	std::cout << text << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

एक String को एक int में Convert करना

stoi (string to int) एक string के leading numeric हिस्से को parse करता है और इसे एक int की तरह return करता है, अगर string एक valid number से शुरू न हो तो एक exception throw करते हुए।

उदाहरण: Converting a String to an int

cpp
// Include std::cout and std::cin
#include <iostream>
// Include std::string
#include <string>

// Program execution starts in main()
int main() {
	std::string input = "42";
	// Declare number and set it to std::stoi(input)
	int number = std::stoi(input);
	// Print to the console with cout
	std::cout << number + 1 << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

एक String को एक double में Convert करना

stod (string to double) stoi जैसा काम करता है लेकिन एक floating-point number parse करता है, एक double return करते हुए, उन decimal values पढ़ने के लिए useful जो originally text की तरह enter या store हुई थीं।

उदाहरण: Converting a String to a double

cpp
// Include std::cout and std::cin
#include <iostream>
// Include std::string
#include <string>

// Program execution starts in main()
int main() {
	std::string input = "3.14";
	// Declare value and set it to std::stod(input)
	double value = std::stod(input);
	// Print to the console with cout
	std::cout << value << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Invalid Conversions Handle करना

stoi और stod दोनों एक std::invalid_argument exception throw करते हैं अगर string बिल्कुल भी एक valid number से शुरू न हो, इसलिए untrusted input से conversions को एक try-catch block में wrap किया जाना चाहिए।

उदाहरण: Handling Invalid Conversions

cpp
// Include std::cout and std::cin
#include <iostream>
// Include std::string
#include <string>

// Program execution starts in main()
int main() {
	std::string bad = "abc";
	// Try code that might throw an exception
	try {
		// Declare number and set it to std::stoi(bad)
		int number = std::stoi(bad);
		// Print to the console with cout
		std::cout << number << std::endl;
	// Handle the exception if one was thrown
	} catch (std::invalid_argument &e) {
		// Print to the console with cout
		std::cout << "Invalid number" << std::endl;
	}
	return 0;
}

Numbers को Strings की तरह Format करना

to_string को string concatenation के साथ combine करना fixed text को computed numeric values के साथ मिलाने वाला एक formatted message बनाने का एक common तरीका है।

उदाहरण: Formatting Numbers as Strings

cpp
// Include std::cout and std::cin
#include <iostream>
// Include std::string
#include <string>

// Program execution starts in main()
int main() {
	// Declare score and set it to 95
	int score = 95;
	std::string message = "Your score: " + std::to_string(score);
	// Print to the console with cout
	std::cout << message << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. stoi("abc") call करना, जो invalid_argument throw करता है।
  2. atoi इस्तेमाल करना और 0 को एक error से न बता पाना।
  3. यह भूल जाना कि to_string को <string> चाहिए।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.