0

I have extracted a number with a decimal place from a string using strtok, 1,2,1.0.

UPDATE The format in this case will be limited from 0.0 up to 1.0. So a single primary & decimal place. The printf is for user feedback & confirmation, the number is passed to a library that requests a number format of n.n. Perhapse I am misinterpreting the message and as a c program only reuires .n if the number is less than one.

I now need to convert the third number to an int with a n.n format that requires a leading zero if less than one.

I have tried a few combinations of int, float, double and atoi(x), strtoumax, but get either 1 or 1.0000.... in the printf.

I need to printf and use the var internally. So what would be the best combination and format to use?

 // third number (this may move position in token order)
 ????? myNumber;

 strcpy(string, strtok( NULL, ","));
 myNumber=atoi(string);
 printf("the number is %?\n", myNumber);
 g_object_set(theItem, "alpha", myNumber, NULL); // accepts n.n only 0.0 -> 1.0
abelenky
  • 63,815
  • 23
  • 109
  • 159
art vanderlay
  • 2,341
  • 4
  • 35
  • 64
  • `atoi` convert string to `int` only. You cannot use it for floating points string number. You should test if string retrieved with strok is a floating point number and use [strtof - strtod](http://en.cppreference.com/w/c/string/byte/strtof) to convert it. – LPs Dec 15 '16 at 15:39
  • [`strtof` or `strtod`](http://en.cppreference.com/w/c/string/byte/strtof)? – Some programmer dude Dec 15 '16 at 15:40
  • 3
    `int`? With a `n.n` format? `int`s don't have a decimal point. Nor leading zeros. Leading zeroes are just something you can display. – Eugene Sh. Dec 15 '16 at 15:40
  • "int with decimal" doesn't make any sense. Please clarify if you need to use `int` or floating point. – Lundin Dec 15 '16 at 15:45
  • @Eugene Sh.: As near as I can tell the OP wants to use decimal fixed-point parsing and formatting, something for which the C standard library lacks built-in support. You will have to piece it together yourself. – doynax Dec 15 '16 at 15:45
  • If your number is an int (***integer***), then the decimal part is always `.0`, yes? Why are you trying to print the decimal part of an integer? Please provide more precise example of what output you want. – abelenky Dec 15 '16 at 15:51
  • What `int` result is expected in the following cases? `"0.0"`, `"0.1"`, `"2.5"`, `"99.0"`, `".0"`, etc? – chux - Reinstate Monica Dec 15 '16 at 15:51
  • @LPs can I limit a floating point to a single decimal place. From reading the link to `strof /d` it states at the bottom of the example `'111.11' -> 111.110000`. The format i need has to be `n.n`, ie `0.5` it will not accept `0.500....`. – art vanderlay Dec 15 '16 at 15:56
  • `%f` format specifier accept precision, so `"%.1f"` do the job for you. – LPs Dec 15 '16 at 15:58

2 Answers2

3

Although your question is unclear, I think what you want is:

double myNumber;  
myNumber = atof(string);    // "0.5" for example
printf("%03.1f", myNumber);  // Output: 0.5

The formatter means:

  • 0 - Pad with leading zeros
  • 3 - Total output should be at least 3 characters (whole-number, decimal, tenths)
    Output may be more if needed
  • .1 - After the decimal, show 1 digit.

If you truly want an int, but to be displayed with decimal places, I suggest:

int myNumber = 3;
printf("%03.1f", (double)myNumber); // Resulting in output: "3.0"

Avoid the whole strtok / atoi / printf issues, and just try these hard-coded calls. One of them should work:

g_object_set(theItem, "alpha", "0.5", NULL); // Param is a string
g_object_set(theItem, "alpha",  0.5,  NULL); // Param is a double
g_object_set(theItem, "alpha",   .5,  NULL); // Param is also a double
g_object_set(theItem, "alpha",    1,  NULL); // Param is an int.

Tell us which one works.

abelenky
  • 63,815
  • 23
  • 109
  • 159
  • not quite what I am after, the input string is in the correct format, ie it is `0.5` or `1.0` or `0.0`. When I pass the string to the library I am using it rejects it as a string and requests a `number` in format of `0.0` – art vanderlay Dec 15 '16 at 16:02
  • 2
    `0.5` will not fit in an Integer. (you do know what an integer is, don't you?) – abelenky Dec 15 '16 at 16:04
  • 1
    @artvanderlay _When I pass the string to the library_...you are passing a int variable not a string.....still unclear what you want to do.. – LPs Dec 15 '16 at 16:07
  • 1
    Is [this](https://developer.gnome.org/gobject/stable/gobject-The-Base-Object-Type.html#g-object-set) the function ? – LPs Dec 15 '16 at 16:10
  • Please add the precise definition of `g_object_set` to your question. – abelenky Dec 15 '16 at 16:10
  • @LPs, my point was if I pass a string as a comment suggested, the library throws an error, requesting an `int of n.n format`. This may be a generic message when they actually mean a `number format of n.n` – art vanderlay Dec 15 '16 at 16:10
  • 1
    @art As far as I can tell abelenky has answered your question. You should be able to work with what he's given you. – cdcdcd Dec 15 '16 at 16:10
  • @cdcdcd, from my question you will see I tried that but the library complained of ` format must be n.n` – art vanderlay Dec 15 '16 at 16:12
  • 2
    @art an int of n.n format doesn't make sense. If it has a decimal point then it is a real number not an integer and in C that means type float. Do you have the prototype for the function? We need to see the actual types of the parameters. – cdcdcd Dec 15 '16 at 16:13
  • 1
    Seriously, stop arguing with us, and explain how an ***INTEGER*** (whole number values only) can have a `.5` in it? Do you mean a floating-point number or an integer? You cannot have it both ways. – abelenky Dec 15 '16 at 16:16
  • @LPs, I am not sure of the origins of this library, but your link seems accurate. This is an image filter that adjusts alpha. not much documentation at all. – art vanderlay Dec 15 '16 at 16:17
  • @LPs, frustrating as it is, the full message returned is `required int of n.n format`. And no documentation is provided in the source. As a bash/php programmer, I find C a complete paradigm shift on how to approach problems. The obvious answer for the experienced C programmers is often not that obvious to newbee's when used to a different programming approach. – art vanderlay Dec 15 '16 at 16:29
  • I've never used GTK+ lib, BTW looking on the web, it seems that g_object_set to set "alpha" parameter is mainly used with float literals, like: `g_object_set(theItem,"alpha",0.5,NULL);` try with this and tell us if it works. – LPs Dec 15 '16 at 16:35
  • Are you using the GObject library? The prototype for the function requires varargs (i.e. vector of null terminated arguments). Does this help? http://stackoverflow.com/questions/18503876/what-is-the-sequence-to-be-followed-to-play-a-song-using-gstreramer – cdcdcd Dec 15 '16 at 16:35
  • @abelenky, `g_object_set(theItem, "alpha", .5, NULL);` actually worked, as did `g_object_set(theItem, "alpha", 1.0, NULL);`, however `g_object_set(theItem, "alpha", 0.5, NULL);` did NOT. So the error message was misleading, and the same each time. Given John Bollinger's explanation below, it seems I was on the right track to start, but let the error message throw me off in a wrong direction. Thankyou for the pointers – art vanderlay Dec 15 '16 at 16:36
  • 1
    There is absolutely NO C-language difference between `g_object_set(theItem, "alpha", .5, NULL);` and `g_object_set(theItem, "alpha", 0.5, NULL);`. `.5` and `0.5` are exactly the same thing. If one is working and the other is not, then there is Dark Magic afoot. – abelenky Dec 15 '16 at 16:38
  • @abelenky, with limited C experience and not really understanding the `g_object_set` command, is it possible that a test of the format before applying the value could trigger an error. that is, the programmer is looking for an exact format match of `1.0` or `.5`? This is inhouse legacy `abandonware` code with lots of generic error messages that are not helpful at all. – art vanderlay Dec 15 '16 at 16:44
0

I have extracted a number with a decimal place from a string using strtok, 1,2,1.0.

I now need to convert the third number to an int with a n.n format that requires a leading zero if less than one.

You are confusing value with presentation. All C integer types represent integral values -- that is, values without any non-zero significant digits having place value less than 1. They therefore do not provide storage for digits with place value less than 1. Moreover, the only non-negative integer less than 1 is 0, which, in a sense, automatically has a leading zero. Thus, no conversion such as you describe is either available or necessary.

On the other hand, if you want to format an integer with trailing (all-zero) decimal places then that's pretty easy, but the result is no longer an int, but a string (if the result is even retained at all). For example:

char decimal_integer[15];
int one_point_oh = 1;

// does the right thing for 0, too:
sprintf(decimal_integer, "%d.0", one_point_oh);

Perhaps you want to retain information about the precision to which your integer value was specified -- that is, number of zeroes after the decimal point. Such information is not naturally representable in any built-in integer or floating-point data type. If you want it, then you must track that data separately.


The same applies to floating-point -- that is, you must not confuse value with presentation there, either. C floating-point data types (float, double, and long double) represent numeric values, but they do not represent their formatting. If you need to associate formatting details with individual values, then you need to track those separately. You account for such details when you format the number for presentation.

Community
  • 1
  • 1
John Bollinger
  • 160,171
  • 8
  • 81
  • 157
  • so if I understand, just because printf shows it as a multi decimal place, that actual format in memory may just be a single decimal place? – art vanderlay Dec 15 '16 at 16:19
  • @artvanderlay, you're beginning to understand. In fact, the internal representation of an integer contains *zero* decimal fraction places. The internal representation of a floating-point number contains a type- and value-dependent number of binary fraction places (not decimal places). Neither is a human-readable sequence of characters. Such a character sequence results from *formatting* the value; it is not an inherent part of the value, which can be formatted in multiple ways. – John Bollinger Dec 15 '16 at 16:26
  • light starts to dawn. Given this info I suspect the library I am trying to use is actually giving a misleading error message and I may have had the numbers correct all along. – art vanderlay Dec 15 '16 at 16:31