উত্তর:
সহ <iomanip>, আপনি std::fixedএবং ব্যবহার করতে পারেনstd::setprecision
এখানে একটি উদাহরণ
#include <iostream>
#include <iomanip>
int main()
{
double d = 122.345;
std::cout << std::fixed;
std::cout << std::setprecision(2);
std::cout << d;
}
এবং আপনি আউটপুট পাবেন
122.34
#define FIXED_FLOAT(x) std::fixed <<std::setprecision(2)<<(x) যা ব্যবহারকে সহজ করে cout<<FIXED_FLOAT(d)
আপনি প্রায় সেখানেই ছিলেন, স্ট্যান্ড :: ব্যবহারের দরকারও ঠিক আছে, http://www.cplsplus.com/references/iostream/manipulators/fixed/ দেখুন
#include <iostream>
#include <iomanip>
int main(int argc, char** argv)
{
float testme[] = { 0.12345, 1.2345, 12.345, 123.45, 1234.5, 12345 };
std::cout << std::setprecision(2) << std::fixed;
for(int i = 0; i < 6; ++i)
{
std::cout << testme[i] << std::endl;
}
return 0;
}
আউটপুট:
0.12
1.23
12.35
123.45
1234.50
12345.00
setprecision(n)সম্পূর্ণ সংখ্যায় প্রযোজ্য, ভগ্নাংশের অংশ নয়। ভগ্নাংশের অংশে এটি প্রয়োগ করার জন্য আপনাকে স্থির-পয়েন্ট ফর্ম্যাটটি ব্যবহার করতে হবে:setiosflags(ios::fixed)
গৃহীত উত্তর সরল করুন
সরলীকৃত উদাহরণ:
#include <iostream>
#include <iomanip>
int main()
{
double d = 122.345;
std::cout << std::fixed << std::setprecision(2) << d;
}
এবং আপনি আউটপুট পাবেন
122.34
রেফারেন্স:
সামঞ্জস্যপূর্ণ ফর্ম্যাটিংয়ের সময় আমার পূর্ণসংখ্যার জন্য একটি সমস্যা ছিল।
সম্পূর্ণতার জন্য একটি পুনর্লিখন:
#include <iostream>
#include <iomanip>
int main()
{
// floating point formatting example
double d = 122.345;
cout << std::fixed << std::setprecision(2) << d << endl;
// Output: 122.34
// integer formatting example
int i = 122;
cout << std::fixed << std::setprecision(2) << double(i) << endl;
// Output: 122.00
}
কোডিং প্রতিযোগিতায় আমার একই সমস্যা ছিল এবং আমি এটিই হ্যান্ডেল করেছি। সমস্ত দ্বিগুণ মান 2 এর যথার্থতা সেট করা
প্রথমে সংজ্ঞা ব্যবহার করতে শিরোনাম যুক্ত করুন
#include <iomanip>
তারপরে আমাদের মূলটিতে নিম্নলিখিত কোড যুক্ত করুন
double answer=5.9999;
double answer2=5.0000;
cout<<setprecision(2)<<fixed;
cout <<answer << endl;
cout <<answer2 << endl;
আউটপুট:
5.99
5.00
৫.০০ লেখার জন্য আপনাকে স্থির ব্যবহার করতে হবে কেন, আপনার আউটপুট ৫.০০ এর জন্য আসে না।
একটি সংক্ষিপ্ত রেফারেন্স ভিডিও লিঙ্ক আমি যুক্ত করছি যা সহায়ক
দশমিক বিন্দুর পরে স্থির 2 অঙ্ক সেট করতে প্রথমে এগুলি ব্যবহার করুন:
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
তারপরে আপনার দ্বিগুণ মানগুলি মুদ্রণ করুন।
এটি একটি উদাহরণ:
#include <iostream>
using std::cout;
using std::ios;
using std::endl;
int main(int argc, char *argv[]) {
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
double d = 10.90;
cout << d << endl;
return 0;
}
টেমপ্লেট সহ
#include <iostream>
// d = decimal places
template<int d>
std::ostream& fixed(std::ostream& os){
os.setf(std::ios_base::fixed, std::ios_base::floatfield);
os.precision(d);
return os;
}
int main(){
double d = 122.345;
std::cout << fixed<2> << d;
}
বৈজ্ঞানিক ক্ষেত্রেও একই, প্রস্থের বিকল্পের সাথেও (কলামগুলির জন্য কার্যকর)
// d = decimal places
template<int d>
std::ostream& f(std::ostream &os){
os.setf(std::ios_base::fixed, std::ios_base::floatfield);
os.precision(d);
return os;
}
// w = width, d = decimal places
template<int w, int d>
std::ostream& f(std::ostream &os){
os.setf(std::ios_base::fixed, std::ios_base::floatfield);
os.precision(d);
os.width(w);
return os;
}
// d = decimal places
template<int d>
std::ostream& e(std::ostream &os){
os.setf(std::ios_base::scientific, std::ios_base::floatfield);
os.precision(d);
return os;
}
// w = width, d = decimal places
template<int w, int d>
std::ostream& e(std::ostream &os){
os.setf(std::ios_base::scientific, std::ios_base::floatfield);
os.precision(d);
os.width(w);
return os;
}
int main(){
double d = 122.345;
std::cout << f<10,2> << d << '\n'
<< e<10,2> << d << '\n';
}
একটি সামান্য বিন্দু; শিরোনামে নিম্নলিখিত রাখুন
নেমস্পেস স্ট্যান্ড ব্যবহার করে;
তারপর
স্টাড :: কাউট << স্টডি :: ফিক্সড << স্টাড :: সেটপ্রেসিশন (২) << d;
সরলীকৃত হয়
cout << স্থির << নির্ধারণ (2) << d;
using namespace std;এটির জন্য ব্যবহার করবেন না - আপনি কেন এমন করছেন তা বুঝতে পারেন।
এটি ম্যাট্রিক্স ব্যবহার করে একটি উদাহরণ।
cout<<setprecision(4)<<fixed<<m[i][j]