Reputation: 2463
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
Upvotes: 0
Views: 1379
Reputation: 64682
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 zeros3
- Total output should be at least 3 characters (whole-number, decimal, tenths).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.
Upvotes: 3
Reputation: 180111
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.
Upvotes: 0