Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# Casting

Posted on 1998-09-23
Medium Priority
361 Views
Hi,

I have an array of float data type of which I tried to change them to round numbers by casting them to unsigned char data type. But for data such as 3.99, the result of the casting is 3. What can I do so that all the data will be rounded to their nearest values?

Rgds,
MY
0
Question by:misumi
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 10

Accepted Solution

rbr earned 20 total points
ID: 1253006
use floor and ceil.

double x=3.99;
char c;

c=(char)ceil(x);

0

LVL 84

Expert Comment

ID: 1253007
It won't be the nearest value if you have

double x=3.01;
char c;

c=(char)ceil(x);
0

Author Comment

ID: 1253008

So, what should I do? Must I use both ceil and floor together?
0

LVL 84

Expert Comment

ID: 1253009
You might consider
c=(char)ceil(x-0.5);
0

LVL 2

Expert Comment

ID: 1253010
Or a variation on ozo's last response:
int n= (int)(your_array_of_floats[index] + 0.5);
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
###### Suggested Courses
Course of the Month9 days, 19 hours left to enroll