Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to printf a long typed value using input size modifier?

This is basically what I am trying to do

// ... some code, calculations, what have you ...
long timeToAdd = returnTimeToAddInLongFormat();

// lets output the long type now, and yes i need the width and precision.
System.out.printf("Time to add: %13.10ld", timeToAdd);

I've read most of the google searches around the topic and think I understand how to do it conceptually, but the JRE keeps throwing me a UnknownFormatConversionException and telling me my input size modifier l doesnt work.

Is there another way to do this, or did I miss something small?

like image 536
snnth Avatar asked Sep 28 '11 16:09

snnth


People also ask

How do I printf a long variable?

To print a long value, use the %ld format specifier. You can use the l prefix for x and o, too. So you would use %lx to print a long integer in hexadecimal format and %lo to print in octal format. C allows both uppercase and lowercase letters for constant suffixes, these format specifiers use just lowercase.

How does printf represent long int?

Long Int Format Specifier %ld The %ld format specifier is implemented for representing long integer values. It is implemented with the printf() function for printing the long integer value stored in the variable.

How do I print an unsigned long?

%ul will just print unsigned (with %u), and then the letter "l" verbatim. Just as "%uw" will print unsigned, followed by letter "w".

How do I print TC size?

We should use “%zu” to print the variables of size_t length. We can use “%d” also to print size_t variables, it will not show any error. The correct way to print size_t variables is use of “%zu”.


1 Answers

Java treats all integer values as d, there is no ld. Even byte and BigInteger is a d type. It also assumes integers have no decimal places. If you want to show 10 zeros, you can convert to double first and use f

like image 131
Peter Lawrey Avatar answered Oct 26 '22 18:10

Peter Lawrey