Jump to content

C++ Count digits in double

Google has failed me again.

 

I have a program that displays data in the form of doubles, however if the double has more than 5 or so digits I want to trim it down to be only 5 digits

Something like this:

int length_var;double var = 43.4556;int cutoff = 5;length_var = var.len(); // <-- no ideafor (var_length >= cutoff) {	remove_last(length_var) // <-- no idea again}

So... I need to find out the number of digits in a double and then figure out how to remove the last digit

Link to comment
https://linustechtips.com/topic/472974-c-count-digits-in-double/
Share on other sites

Link to post
Share on other sites

All digits in total?

Or just the ones after '.' ?

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to comment
https://linustechtips.com/topic/472974-c-count-digits-in-double/#findComment-6339957
Share on other sites

Link to post
Share on other sites

NEW PC build: Blank Heaven   minimalist white and black PC     Old S340 build log "White Heaven"        The "LIGHTCANON" flashlight build log        Project AntiRoll (prototype)        Custom speaker project

Spoiler

Ryzen 3950X | AMD Vega Frontier Edition | ASUS X570 Pro WS | Corsair Vengeance LPX 64GB | NZXT H500 | Seasonic Prime Fanless TX-700 | Custom loop | Coolermaster SK630 White | Logitech MX Master 2S | Samsung 980 Pro 1TB + 970 Pro 512GB | Samsung 58" 4k TV | Scarlett 2i4 | 2x AT2020

 

Link to comment
https://linustechtips.com/topic/472974-c-count-digits-in-double/#findComment-6339960
Share on other sites

Link to post
Share on other sites

All of them.

So, for 1.547 , the length would be 4. For 12345.6789 , the length would be 9. For 1234.0 the length would be 4 or 5?

 

Or do you just want to write the doubles only with the first 5 digits?

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to comment
https://linustechtips.com/topic/472974-c-count-digits-in-double/#findComment-6339976
Share on other sites

Link to post
Share on other sites

You have stronger google-fu but I'm hoping theres an easier and cleaner solution

Link to comment
https://linustechtips.com/topic/472974-c-count-digits-in-double/#findComment-6339990
Share on other sites

Link to post
Share on other sites

So, for 1.547 , the length would be 4. For 12345.6789 , the length would be 9. For 1234.0 the length would be 4 or 5?

 

Or do you just want to write the doubles only with the first 5 digits?

I want to remove the last few digits

And yes, that's correct 53.1245 would be length of 6

Link to comment
https://linustechtips.com/topic/472974-c-count-digits-in-double/#findComment-6339993
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×