-1

Here is my problem. I have a vector suct that vector and after some operations I'll store some double values into this vector.However,I want they all have same decimal point digit. I mean if first value to be stored is 4.143 and the second one is 67.32 and the third one is 165 then I want to store them like 004.143 and 067.320 *165.000*.Is there any way to do that? Thanks in advance

caesar
  • 2,865
  • 11
  • 29
  • 36

2 Answers2

1

You seem to be confusing how they are stored with how they are displayed.

When you display the values, you can use, for example, String.format() or System.out.printf() to format the value to a specified width and number of decimal places.

For example:

double a = 4.143;
double b = 67.32;
double c = 165;
System.out.printf("%07.3f %07.3f %07.3f", a, b, c);

Prints:

004.143 067.320 165.000

Edit: So it's C++ (sorry; I've been answering Java questions all day). The same holds, except printf() or stringstream or ostream is used to format instead. The concepts are the same.

As for your comments below: "034.1" and "34.1" are the same value. They are stored the same in either language. Padding 0's are a function of display, not storage. A radix sort traditionally involves integers; you could convert your values to integers by e.g. multiplying by 1000 (or whatever), then just use math to get the digits. For example:

double value1 = 0.124; 
double value2 = 53.0;

printf("%i\n", (int)(value1 * 1000.0) % 10);
printf("%i\n", (int)(value2 * 1000.0) % 10);

Prints 4 and 0 (the thousandths digit). For your radix sort, when comparing two keys, compute and compare digits appropriately.

Jason C
  • 38,729
  • 14
  • 126
  • 182
  • ,I've asked it because, I'll use these values in a radix sort.So,they need to have a same number of digits. – caesar Nov 17 '13 at 04:11
  • I'm interested in how they're stored rather than how they're displayed as I said – caesar Nov 17 '13 at 04:12
  • and sorry for missunderstanding,Its a c++ question not a java – caesar Nov 17 '13 at 04:13
  • How they are stored is how they are stored. Leading 0's are a characteristic of display, not storage. "034" and "34" are the same value and are stored the same. Also, a radix sort is generally done with integers, and you can pull out digits using math (e.g. `(value / 100) % 10`. One option for doubles is to sort by their actual value, or to format them as fixed width strings and sort alphabetically. The former makes more sense. – Jason C Nov 17 '13 at 04:14
  • I have to sort floating points no argue that. – caesar Nov 17 '13 at 04:15
  • @caesar The answer is the same in either language, except you use `printf` or something from `sstream` or `ostream` to format. In C++ as well as Java, the stored value is unrelated to padding 0's. In both languages, what I just said about radix sort is true. – Jason C Nov 17 '13 at 04:15
  • Ok then what's the problem with sorting by their values? 165 > 67.32 > 4.143 no matter how many padding 0's you add. – Jason C Nov 17 '13 at 04:16
  • Dont I have to have same number of digits for all values to be compared in radix sort ? – caesar Nov 17 '13 at 04:17
  • Sure but just compute the digit. `(int)(doubleValue * 1000.0) % 10` will extract 4 from 0.134. – Jason C Nov 17 '13 at 04:20
1

The float type is named that way, because it represents a floating point number. If you wan't to store numbers with fixed-position, you have to design your own class for that (or use one that someone else made). There's no other way.

But - if you're only concerned about the fact that floating point numbers are stored differently in memory, dependant on the point position, then you're wrong. No matter where the dot is, the coding is the same. You should read about the IEEE 754 standard.

You could also use a stringstream in order to write a float into a std::string with fixed precision.

And of course if you're just concerned about the display, other people already made that clear in their replies.

Paweł Stawarz
  • 3,952
  • 2
  • 17
  • 26
  • The OP appears to be trying to implement a radix sort and is under the impression that storage/display affects the ability to extract digits from the number. What he *should* be doing is using math to operate on the values and extract the relevant digits, instead of being concerned with storage/display. – Jason C Nov 17 '13 at 04:35
  • 1
    Whatever the OP is concered about, is explained in this post, so he can pick whatever option he wants. I've already tried helping someone understand something in the correct way on this site, and it's not worth. People want precise answers. If they wanted to understand, they would learn and try, instead of immediately asking. There's no point. No matter if it's fixed or floating of convincing anyone :) – Paweł Stawarz Nov 17 '13 at 04:39