मेरे पास 223.45654543434
जैसे डबल नंबर हैं और मुझे इसे 0.223x10e+2
जैसे दिखाना होगा।वैज्ञानिक नोटेशन में प्रारूप डबल मान
मैं जावा में यह कैसे कर सकता हूं?
मेरे पास 223.45654543434
जैसे डबल नंबर हैं और मुझे इसे 0.223x10e+2
जैसे दिखाना होगा।वैज्ञानिक नोटेशन में प्रारूप डबल मान
मैं जावा में यह कैसे कर सकता हूं?
अंत में मैं हाथ से कार्य करें:
public static String parseToCientificNotation(double value) {
int cont = 0;
java.text.DecimalFormat DECIMAL_FORMATER = new java.text.DecimalFormat("0.##");
while (((int) value) != 0) {
value /= 10;
cont++;
}
return DECIMAL_FORMATER.format(value).replace(",", ".") + " x10^ -" + cont;
}
लगातार विभाजन !? नीचे दिए गए उत्तर को देखें – ErstwhileIII
उत्तर कई कारकों के आधार पर "नीचे", "उपरोक्त" या बस कहीं और हो सकते हैं, उदाहरण के लिए आप उन्हें कैसे क्रमबद्ध करते हैं (सबसे सक्रिय, सबसे पुराना, सबसे अधिक वोट दिया गया)। यदि आपको किसी अन्य उत्तर का संदर्भ देने की आवश्यकता है, तो उससे लिंक करें (उत्तर पर परमालिंक प्राप्त करने के लिए 'साझा करें' पर क्लिक करें) – Grodriguez
ओह नहीं नहीं नहीं !! –
Display numbers in scientific notation से।
आप java.text
पैकेज का उपयोग वैज्ञानिक अंकन में संख्या प्रदर्शित कर सकते हैं (कॉपी/चिपकाने क्योंकि पेज मुद्दों कर किया जा रहा है)। विशेष रूप से DecimalFormat
java.text
पैकेज में इस उद्देश्य के लिए उपयोग किया जा सकता है।
निम्न उदाहरण से पता चलता है कि यह कैसे करना है:
2.235e+02
में
import java.text.*;
import java.math.*;
public class TestScientific {
public static void main(String args[]) {
new TestScientific().doit();
}
public void doit() {
NumberFormat formatter = new DecimalFormat();
int maxinteger = Integer.MAX_VALUE;
System.out.println(maxinteger); // 2147483647
formatter = new DecimalFormat("0.######E0");
System.out.println(formatter.format(maxinteger)); // 2,147484E9
formatter = new DecimalFormat("0.#####E0");
System.out.println(formatter.format(maxinteger)); // 2.14748E9
int mininteger = Integer.MIN_VALUE;
System.out.println(mininteger); // -2147483648
formatter = new DecimalFormat("0.######E0");
System.out.println(formatter.format(mininteger)); // -2.147484E9
formatter = new DecimalFormat("0.#####E0");
System.out.println(formatter.format(mininteger)); // -2.14748E9
double d = 0.12345;
formatter = new DecimalFormat("0.#####E0");
System.out.println(formatter.format(d)); // 1.2345E-1
formatter = new DecimalFormat("000000E0");
System.out.println(formatter.format(d)); // 12345E-6
}
}
* "प्रतिलिपि/चिपकाना क्योंकि पृष्ठ में समस्याएं हैं" * इसके अलावा क्योंकि बाहरी संदर्भों के चलते, दूर जाना आदि, इसलिए स्वयं को स्वयं ही खड़ा होना चाहिए। –
पर्याप्त सच है, हालांकि मैं आमतौर पर वास्तव में केवल प्रासंगिक भागों को सी/पी करता हूं और फिर अधिक विस्तृत जानकारी के लिए मूल स्रोत से लिंक करता हूं। इस मामले में, मूल स्रोत अच्छी तरह से काम नहीं कर रहा है, इसलिए मैंने इसे सब कुछ यहां रखा है। –
System.out.println(String.format("%6.3e",223.45654543434));
परिणाम जो सबसे करीब मैं हो रही है।
अधिक जानकारी: http://java.sun.com/j2se/1.5.0/docs/api/java/util/Formatter.html#syntax
या 'System.out.printf ("% 6.3e \ n", 223.23525) ' –
कृपया बताएं कि"% 6.3e "का अर्थ क्या है। मैं इसे घटा सकता हूं .3e का मतलब है कि इसके बाद केवल तीन अंक हैं। लेकिन 6 मतलब क्या है? –
@DonLarynx का मतलब है कि परिणाम में अधिकतम 6 अर्थपूर्ण अंक हैं। अधिक जानकारी के लिए फॉर्मेटर के लिए वाक्यविन्यास के लिंक की जांच करें। –
यहाँ एक धागा सुरक्षित दिया गया है:
private static final AtomicReference<NumberFormat> decimalFormatter = new AtomicReference<>();
private static final int PRECISION = 9;
static
{
decimalFormatter.set(new DecimalFormat("0.0E0"));
decimalFormatter.get().setRoundingMode(RoundingMode.HALF_UP);
decimalFormatter.get().setMinimumFractionDigits(PRECISION);
}
public static String format(final BigInteger integer)
{
if (null == integer) return null; // could throw NullPointerException here, up to you
String output = decimalFormatter.get().format(integer).toLowerCase();
final String sign = output.indexOf('-') >= 0 ? "-" : "+";
if (output.substring(output.indexOf('e')+1).length() < 2) output = output.replace("e", "e" + sign + "0");
else output = output.replace("e", "e" + sign);
return output;
}
यह धागा सुरक्षित नहीं है; प्रत्येक धागे का अपना फॉर्मेटर होना चाहिए। 'परमाणु संदर्भ <>' में एक साझा उदाहरण लपेटना कुछ भी प्राप्त नहीं करता है। आपको या तो 'थ्रेडलोकल' का उपयोग करने की आवश्यकता है या हर बार जब आपका 'प्रारूप()' विधि कहा जाता है तो एक नया फॉर्मेटर बनाएं। –
इस उत्तर 40k + लोगों के लिए समय की बचत होगी, जो "जावा वैज्ञानिक नोटेशन" गुगल रहे हैं।
%X.YE
में वाई का क्या मतलब है?
.
और E
के बीच संख्या दशमलव स्थानों (नहीं महत्वपूर्ण आंकड़े) की संख्या है।
System.out.println(String.format("%.3E",223.45654543434));
// "2.235E+02"
// rounded to 3 decimal places, 4 total significant figures
String.format
विधि को पूर्णांक बनाना दशमलव अंक की संख्या निर्दिष्ट करने की आवश्यकता है। यदि आपको मूल संख्या के सटीक महत्व को संरक्षित करने की आवश्यकता है तो आपको एक अलग समाधान की आवश्यकता होगी।
एक्स का मतलब %X.YE
में क्या है?
%
और .
के बीच संख्या स्ट्रिंग तक का समय लग जाएगा पात्रों में से न्यूनतम संख्या है।
System.out.println(String.format("%3.3E",223.45654543434));
// "2.235E+02" <---- 9 total characters
System.out.println(String.format("%9.3E",223.45654543434));
// "2.235E+02" <---- 9 total characters
System.out.println(String.format("%12.3E",223.45654543434));
// " 2.235E+02" <---- 12 total characters, 3 spaces
System.out.println(String.format("%12.8E",223.45654543434));
// "2.23456545E+02" <---- 14 total characters
System.out.println(String.format("%16.8E",223.45654543434));
// " 2.23456545E+02" <---- 16 total characters, 2 spaces
उचित वैज्ञानिक संकेतन में नंबर 2.23e-2 हो जाएगा, नहीं 0.223e-3 (इस संख्या के रूप में स्ट्रिंग ऊपर दिखाए गए स्वचालित रूप से अगर आप इसे बाहर छोड़ भर जाएगा, आवश्यक नहीं है)। – kennytm
... और निश्चित रूप से मेरा मतलब है 2.23e + 2, 2.23e-2 नहीं। मेरा दावा है कि ऐसा इसलिए है क्योंकि '3' ओपी' -' की तुलना में नोटिस करना आसान है। – kennytm