Win a copy of Terraform in Action this week in the Cloud forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Limiting length of int64 in Go

 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I need to work with int64 that are no longer than 16 digits and they are generated by an application I cannot control. Just before processing them I want to check wether they are equal or bigger than 17 digits and then limit them to 16 max. I have a small util that checks the length of the int:



Having checked the length, I am doing the following:



my issue is that I cannot always remove 3 as the int64 generated might be already of length 17 and I need to only remove 1 digit. Can you help? Is also advisable to convert it to a string operate on the string and convert it back?


Thank you
 
Ranch Hand
Posts: 63
1
Android Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Isn't the modulo or division reminder that what you are looking for?
10^16 = 10000000000000000 is the first uint64 number with length of 17 or longer. The reminder of the division num / 10^16 as decimal digit string has maximal 16 digits.

reply
    Bookmark Topic Watch Topic
  • New Topic