开发者

Filter some type of data in float

开发者 https://www.devze.com 2023-04-09 01:06 出处:网络
how to filter out float values with only zeroes beyond decimal & others having some non-zero values beyond decimal too.

how to filter out float values with only zeroes beyond decimal & others having some non-zero values beyond decimal too.

for example.

13.开发者_运维技巧000000 13.120001

i want it like this:

13.0 13.120001


If your application stores the floating point values in something like C/C++'s float's or double's, you may have a problem here.

First of all, float/double in the majority of cases represents values using base-2 and when you do something like double x = 0.1;, x in fact will never be equal to 0.1, but it will be very close to 0.1. That is because not every decimal (base-10) fraction can be represented exactly in base-2.

When you print or convert that x to a string using one of the printf-like functions, the resultant string can vary from something like 0.099999999 to 0.1 to 0.10000000000000000555. The result will depend on what's in x and on how you convert it to a string (how many digits you allow for the integer part, fractional part or all).

Generally there's no one-size-fits-all solution here. In one case the exactness and rounding issues can be unimportant, while in another they can be the most important. When performance is more important than the exact representation of decimal fractions, you use base-2 types. Otherwise, for example, when you're counting money, you use special types (and you may first need to construct them) that can represent decimal fractions exactly and then you avoid some of the described issues at a cost of extra computation time and maybe storage.

You may sometimes use fixed-point types and arithmetic. For example, if your numbers should not have more than 3 fractional digits and aren't too big, you can use scaled integers:

long x = 123456; // x represents 123.456

You add, subtract and compare them just as regular integers, but you multiply and divide them differently, for example like this:

long x = 123456; // x represents 123.456
long y = 12345;  // y represents 12.345
long p = (long)(((long long)x * y) / 1000); // p=1524064, representing x*y=1524.064
long q = (long)((long long)x * 1000) / y);  // q=10000, representing x/y=10.000

And in this case it is relatively easy to figure out the digits of the fractional part, just look at (x%1000)/100, (x%100)/10 and x%10.


split 13.00000 or 13.20001 into 2 values like in 13.0000 as 13 and 0000 ...so compare 2nd value whether it is greater than 0 (ex:13.20001 where 2001>0) if its yes then set same value . else (ex 13.0000 where 0000=0) then set %.2f=> 13.00

0

精彩评论

暂无评论...
验证码 换一张
取 消