Java NumberFormat.getInstance() method

Samual Sam
Updated on 26-Jun-2020 09:12:20

295 Views

The java.text.NumberFormat class is used for formatting numbers and currencies as per a specific Locale. Number formats varies from country to country.To get the instance of the NumberFormat class, call getInstance() method in Java.First, we will use the getInstance() method.NumberFormat n = NumberFormat.getInstance();Now, we will format a double.double val = 1.9898798; String formattedVal = n.format(val);The following is the final example.Example Live Demoimport java.text.NumberFormat; public class Demo {     public static void main(String args[]) {        NumberFormat n = NumberFormat.getInstance();        double val = 1.9898798;        System.out.println("Value: "+val);        String formattedVal = n.format(val);        System.out.println("Formatted Value: "+formattedVal);     } }OutputValue: 1.9898798 Formatted Value: 1.99Read More

Swapping two variable value without using third variable in C/C++

Samual Sam
Updated on 26-Jun-2020 09:12:20

380 Views

The following is an example of swapping two variables.Example Live Demo#include int main() {    int a, b;    printf("Enter the value of a : ");    scanf("%d", &a);    printf("Enter the value of b : ");    scanf("%d", &b);    a += b -= a = b - a;    printf("After Swapping : %d\t%d", a, b);    return 0; }OutputEnter the value of a : 23 Enter the value of b : 43 After Swapping : 4323In the above program, two variables a and b are declared and initialized dynamically at run time.int a, b; printf("Enter the value of ... Read More

Why are global and static variables initialized to their default values in C/C++?

karthikeya Boyini
Updated on 26-Jun-2020 09:11:54

2K+ Views

Global and static variables are initialized to their default values because it is in the C or C++ standards and it is free to assign a value by zero at compile time. Both static and global variable behave same to the generated object code. These variables are allocated in .bss file and at the time of loading it allocates the memory by getting the constants alloted to the variables.The following is an example of global and static variables.Example Live Demo#include int a; static int b; int main() {    int x;    static int y;    int z = 28; ... Read More

Why does C++ require a cast for malloc() but C doesn't?

Samual Sam
Updated on 26-Jun-2020 09:11:25

620 Views

In C language, the void pointers are converted implicitly to the object pointer type. The function malloc() returns void * in C89 standard. In earlier versions of C, malloc() returns char *. In C++ language, by default malloc() returns int value. So, the pointers are converted to object pointers using explicit casting.The following is the syntax of allocating memory in C language.pointer_name = malloc(size);Here, pointer_name − Any name given to the pointer.size − Size of allocated memory in bytes.The following is an example of malloc() in C language.Example Live Demo#include #include int main() {    int n = 4, ... Read More

Set the base time zone offset to GMT in Java

Ankith Reddy
Updated on 26-Jun-2020 09:11:08

780 Views

In order to set the base time zone to GMT in Java, we use the setRawOffset(int offsetMillis) method. The java.util.TimeZone.setRawOffset(int offsetMillis) method set the base timezone offset to GMT.Declaration − The java.util.TimeZone.setRawOffset(int offsetMillis) method is declared as follows −public abstract void setRawOffset(int offsetMillis)where offsetMillis is the given base time zone offset to GMT.Let us set the base timezone offset to GMT in Java −Example Live Demoimport java.util.*; public class Example {    public static void main( String args[] ) {       // creating default object of TimeZone       TimeZone obj = TimeZone.getDefault();       System.out.println("Default timezone ... Read More

Java NumberFormat.getCurrencyInstance() method

karthikeya Boyini
Updated on 26-Jun-2020 09:10:36

2K+ Views

The getCurrencyInstance() method of the NumberFormat class returns the instance of the NumberFormat class. The java.text.NumberFormat class is used for formatting numbers and currencies as per a specific Locale. Number formats varies from country to countryHere, we have considered a locale.NumberFormat n = NumberFormat.getCurrencyInstance(Locale.FRANCE);Then, we have formatted a double value with the currency.double points = 1.78; System.out.println(n.format(points));The following is the final example.Example Live Demoimport java.text.NumberFormat; import java.util.Locale; public class MainClass {     public static void main(String[] args) {        // Currency of France is Euro        NumberFormat n = NumberFormat.getCurrencyInstance(Locale.FRANCE);        // points        double points = 1.78;        double totalPoints = points * 1000;        System.out.println(n.format(points));     ... Read More

How to return an array from a function in C++?

karthikeya Boyini
Updated on 26-Jun-2020 09:10:29

263 Views

C++ does not return entire array but it can return pointer to an array. Outside the function, address of local variable cannot be returned. By making local variable static, it can return the address of local variable.The following is the syntax to return a pointer.int * function_name() { body }Here,function_name − The name of function given by user.The following is an example to return an array from a function.Example Live Demo#include using namespace std; int * ret() {    static int x[3];    for(int i=0 ; i

Substitute tokens in a String in Java

George John
Updated on 26-Jun-2020 09:10:28

608 Views

To substitute tokens in a String in Java, we use the Message Format class. The Message Format class provides a means to produce concatenated messages which are not dependent on the language. The Message Format class extends the Serializable and Cloneable interfaces.Declaration − The java.text.MessageFormat class is declared as follows −public class MessageFormat extends FormatThe MessageFormat.format(pattern, params) method formats the message and fills in the missing parts using the objects in the params array matching up the argument numbers and the array indices.The format method has two arguments, a pattern and an array of arguments. The pattern contains placeholder in ... Read More

How to convert a single char into an int in C++

Samual Sam
Updated on 26-Jun-2020 09:09:53

501 Views

The following is an example to convert a character into int.Example Live Demo#include using namespace std; int main() {    char c = '8';    int i = c - 48;    cout

Java NumberFormat.getPercentageInstance() method

Samual Sam
Updated on 26-Jun-2020 09:09:02

282 Views

The java.text.NumberFormat class is used for formatting numbers and currencies as per a specific Locale. Number formats varies from country to countryTo format a number as percentage you need a percentage NumberFormat instance. To achieved this, use the getPercentageInstance() method.Example Live Demoimport java.text.NumberFormat; import java.util.Locale; public class MainClass {    public static void main(String[] args) {        // Currency of France is Euro        NumberFormat n = NumberFormat.getPercentInstance(Locale.FRANCE);        // points        double points = 1.78;        double totalPoints = points * 1000;        System.out.println(n.format(points));        System.out.println(n.format(totalPoints));     } }Output178 % 178 000 %

Advertisements